Python 死鎖現象
阿新 • • 發佈:2019-01-14
reading oca __main__ art sta threading int tar get
import time from threading import Thread,Lock,RLock def f1(locA,locB): locA.acquire() print(‘f1>>1號搶到了A鎖‘) time.sleep(1) locB.acquire() print(‘f1>>1號搶到了B鎖‘) locB.release() locA.release() def f2(locA,locB): locB.acquire() print(‘f2>>2號搶到了B鎖‘) locA.acquire() time.sleep(1) print(‘f2>>2號搶到了A鎖‘) locA.release() locB.release() if __name__ == ‘__main__‘: locA = Lock() locB = Lock() t1 = Thread(target=f1,args=(locA,locB)) t2 = Thread(target=f2,args=(locA,locB)) t1.start() t2.start()
Python 死鎖現象