import threading
import time
number = 0
lock = threading.RLock() #是Lock()的升级版,用Rlock()即可
def run(num):
lock.acquire() #跟cpu申请一把锁
global number
number +=1
print number
lock.release() #释放锁
time.sleep(1)
#lock.release() 如果放在这里,将是串行输出,每个线程独占cpu的时间,达不到并行效果
for i in range(20):
t = threading.Thread(target=run,args=(i,))
t.start()