forked from SergioJune/python_test
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest33.py
65 lines (48 loc) · 1.31 KB
/
test33.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
'练习多线程'
__author__ = 'sergiojune'
import threading
import multiprocessing
def run():
print('thread %s is running' % threading.current_thread().name)
for x in range(5):
print('thread %s ==> %d' % (threading.current_thread().name, x))
print('thread %s end' % threading.current_thread().name)
print('thraed %s is running' % threading.current_thread().name)
t = threading.Thread(target=run, name='loopthread')
# 开启线程
t.start()
t.join()
print('thread %s is end' % threading.current_thread().name)
# 多线程的锁
balance = 0
def change(n):
global balance
balance = balance + n
balance = balance - n
print(balance)
def run_thread():
l = threading.Lock()
for x in range(1, 100000):
try:
# 获取锁
l.acquire()
change(x)
finally:
# 释放锁
l.release()
t1 = threading.Thread(target=run_thread)
t2 = threading.Thread(target=run_thread())
t1.start()
t2.start()
t1.join()
t2.join()
print('end')
def loop():
x = 0
while True:
x = x ^ 1
for x in range(multiprocessing.cpu_count()): # 根据cpu的数量来开线程
t = threading.Thread(target=loop)
t.start()