-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1115.py
More file actions
70 lines (54 loc) · 1.74 KB
/
1115.py
File metadata and controls
70 lines (54 loc) · 1.74 KB
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
import time,threading
# new loop code:
def loop(): #定义程序
print ('thread %s is running...' % threading.current_thread().name) # 打印程序开始
n = 0
while n<5:
n = n+1
print ('thread %s >>> %s' % (threading.current_thread().name,n))
time.sleep(1) # 挂起1秒
print ('thread %s ended.' % threading.current_thread().name)
print ('thread %s is running...' % threading.current_thread().name) # 打印开始
t = threading.Thread(target = loop, name = 'LoopThread') # 指定线程运行 loop程序,以及线程名
t.start() # 开始
t.join() # 结束
print ( 'thread %s ended.' % threading.current_thread().name) # 打印结束
balance = 0
lock = threading.Lock()
def change_it(n): # 先存后取,结果应该为0
global balance
balance = balance + n
#print ('balance is -> ',balance,'args is -> ',n)
balance = balance - n
#print ('balance is -> ',balance,'args is -> ',n)
def run_thread(n):
for i in range (100000):
lock.acquire()
try:
change_it(n)
finally:
lock.release()
t1 = threading.Thread(target = run_thread,args = (5,))
t2 = threading.Thread(target = run_thread,args = (8,))
t1.start()
t2.start()
t1.join()
t2.join()
print (balance)
import threading
# 创建全局ThreadLocal对象:
local_school = threading.local()
def process_student():
# 获取当前线程关联的student:
std = local_school.student
print('Hello, %s (in %s)' % (std, threading.current_thread().name))
def process_thread(name):
# 绑定ThreadLocal的student:
local_school.student = name
process_student()
t1 = threading.Thread(target= process_thread, args=('Alice',), name='Thread-A')
t2 = threading.Thread(target= process_thread, args=('Bob',), name='Thread-B')
t1.start()
t2.start()
t1.join()
t2.join()