线程锁

import threading,time
num = 1
lock = threading.Lock() #申请一把锁

def run():
time.sleep(1)
global num
lock.acquire() #加锁
num+=1
lock.release() #解锁
ts = []
for i in range(100):
t = threading.Thread(target=run)
t.start()
ts.append(t)
[t.join() for t in ts]
print(num)




posted on 2018-05-30 17:01  公子兔  阅读(87)  评论(0编辑  收藏  举报

导航