RLock 解决死锁

import threading
import time

lock =threading.RLock()


class Mythread(threading.Thread):
def __init__(self,name):
super(Mythread,self).__init__()
self.name = name
def doA(self):
lock.acquire()
print(self.name, "gotlockA", time.ctime())
time.sleep(3)
lock.acquire()
print(self.name, "gotlockB", time.ctime())
lock.release()
lock.release()

def doB(self):
lock.acquire()
print(self.name, "gotlockA", time.ctime())
time.sleep(3)
lock.acquire()
print(self.name, "gotlockB", time.ctime())
lock.release()
lock.release()
def run(self):

self.doA()
self.doB()

if __name__ == '__main__':
t1 = Mythread("thread-1")
t2 = Mythread("thread-2")
t1.start()
t2.start()

猜你喜欢

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