标签:def AC pen 线程锁 style 结束 pre 守护 glob
import threading,time
num=1
lock=threading.Lock() #申请一把锁
def run():
time.sleep(1)
global num
lock.acquire() #加锁 #python3里面不加也无所谓,python2里面要加锁
num+=1
lock.release() #解锁
ts=[]
for i in range(50):
t = threading.Thread(target=run)
t.start()
ts.append(t)
[t.join() for t in ts]
print(num)
# 守护线程
# 只要主线程结束,那么子线程立即结束,不管子线程有没有运行完成
标签:def AC pen 线程锁 style 结束 pre 守护 glob
原文地址:https://www.cnblogs.com/jiadan/p/9120854.html