`from threading import Thread
import time
n = 100
def task():
global n
tmp = n
time.sleep(1) # 进入IO,GIL锁会释放,则n为99,当没有IO则n为0
n = tmp -1
t_list = []
for i in range(100):
t = Thread(target=task)
t.start()
t_list.append(t)
for t in t_list:
t.join()
print(n)`
点击查看代码
from threading import Thread
import time
n = 100
def task():
global n
tmp = n
time.sleep(1) # 进入IO,GIL锁会释放,则n为99,当没有IO则n为0
n = tmp -1
t_list = []
for i in range(100):
t = Thread(target=task)
t.start()
t_list.append(t)
for t in t_list:
t.join()
print(n)