死锁现象

import threading
import time


lockA = threading.Lock()
lockB = threading.Lock()
class Mythread(threading.Thread):
def __init__(self,name):
super(Mythread,self).__init__()
self.name = name
def doA(self):
lockA.acquire()
print(self.name, "gotlockA", time.ctime())
time.sleep(3)
lockB.acquire()
print(self.name, "gotlockB", time.ctime())
lockB.release()
lockA.release()
def doB(self):
lockB.acquire()
print(self.name, "gotlockA", time.ctime())
time.sleep(3)
lockA.acquire()
print(self.name, "gotlockB", time.ctime())
lockA.release()
lockB.release()
def run(self):
self.doA()
self.doB()
if __name__ == '__main__':
t1 = Mythread("thread-1")
t2 = Mythread("thread-2")
t1.start()
t2.start()


解决办法:
threading.Lock() 改成threading.RLock

猜你喜欢

转载自www.cnblogs.com/knowlearner/p/10404099.html