上一篇我们初步讲解了关于多线程的创建,启动,以及锁的运用
今天我们继续看关于锁的更多内容,递归锁,递归锁和Rlock,使用和lock差不多但是有一点,当rlock的调用数量大于0,其它线程将无法得到锁,
我们先看一下为什么引进递归锁
import threading
import time
#死锁
class Threads(threading.Thread):
def actionA(self):
A.acquire()
print(self.name,"gotA",time.ctime())
time.sleep(1)
B.acquire()
print(self.name,"gotB",time.ctime(1))
time.sleep(2)
B.release()
A.release()
def actionB(self):
B.acquire()
print(self.name,"gotB",time.ctime())
time.sleep(1)
A.acquire()
print(self.name,"gotA",time.ctime(1))
time.sleep(2)
A.release()
B.release()
def run(self):
self.actionA()
self.actionB()
if __name__ == '__main__':
L=[]
A=threading.Lock()
B=threading.Lock()
for i in range(5):