import threading标签:play,Thread,start,lock,args,threading,num,线程,使用 From: https://blog.51cto.com/u_13137233/5968084
import time
lock = threading.Lock()
num = 0
lock = threading.Lock()
def play(name):
global num
for i in range(100):
print(f"{name}正在工作{i}")
lock.acquire()
a = num
time.sleep(0.1)
a = a+1
num = a
lock.release()
t1 = threading.Thread(target=play, args=("tom",))
t2 = threading.Thread(target=play, args=("jerry",))
t3 = threading.Thread(target=play, args=("bob",))
t4 = threading.Thread(target=play, args=("kid",))
t1.start()
t2.start()
t3.start()
t4.start()
t1.join()
t2.join()
t3.join()
t4.join()
print("res", num)