Python 死锁现象

时间:2024-01-14 16:32:23
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()