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()
posted @ 2019-02-20 00:53  ★小钻风★  阅读(448)  评论(0)    收藏  举报