07 递归锁
import time from threading import Thread, Lock, RLock def f1(locA, locB): # print('xxxx') # time.sleep(0.1) locA.acquire() print('f1>>1号抢到了A锁') time.sleep(1) locB.acquire() print('f1>>1号抢到了B锁') locB.release() locA.release() def f2(locA, locB): print('22222') time.sleep(0.1) locB.acquire() print('f2>>2号抢到了B锁') locA.acquire() time.sleep(1) print('f2>>2号抢到了A锁') locA.release() locB.release() if __name__ == '__main__': # locA =locB = Lock() locA = Lock() locB = Lock() # locA = locB = RLock() #递归锁,维护一个计数器,acquire一次就加1,release就减1 t1 = Thread(target=f1, args=(locA, locB)) t2 = Thread(target=f2, args=(locA, locB)) t1.start() t2.start()