1 import threading 2 import time 3 num=100 4 def addNum(): 5 global num #在每個執行緒中都獲取這個全域變數 6 r.acquire()#加鎖 7 temp=num 8 time.sleep(0.000001) 9 num=temp-1 10 r.release()#釋放鎖 11 12 thread_list=[] 13 r=threading.Lock()#創建同步鎖 14 for i in range(5): 15 t = threading.Thread(target=addNum) 16 t.start() 17 thread_list.append(t) 18 for t in thread_list:#等待所有執行緒執行完畢 19 t.join() 20 print('final num:',num)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/152398.html
標籤:Python
下一篇:pyhton實作世界疫情地圖
