# Deadlock import threading import time # Example 1、 Deadlock """ 1、 Use two synchronous locks A and B, Synchronous lock a lock can only be called once at the same time ( Lock it once , Until it's unlocked , To lock again ) 2、 class MyThread There are two ways , Separately A Method : Lock A-> Lock B-> Unlock A-> Unlock B,B Method : Lock B-> Lock A-> Unlock B-> Unlock A 3、 adopt sleep simulation IO Blocking 4、 After locking , The code in the lock will not be unlocked until it is executed , If it is not unlocked, it will be blocked , Waiting to unlock 5、 In this example, after a period of execution ,A Method used A lock , To perform locking B The operation of , But found B Method used B lock , To perform the A Lock , And then A Lock and B The locks are blocked , The following unlocking operation cannot be performed , Causes the deadlock thread not to continue execution . """ class MyThred(threading.Thread): def a(self): lockA.acquire() # Lock A print(self.name, ' Lock A', time.ctime()) time.sleep(3) lockB.acquire() # Lock B print(self.name, ' Lock B', time.ctime()) lockA.release() # Unlock A print(self.name, ' Unlock A', time.ctime()) lockB.release() # Unlock B print(self.name, ' Unlock B', time.ctime()) def b(self): lockB.acquire() # Lock B print(self.name, ' Lock B', time.ctime()) time.sleep(3) lockA.acquire() # Lock A print(self.name, ' Lock A', time.ctime()) lockB.release() # Unlock B print(self.name, ' Unlock B', time.ctime()) lockA.release() # Unlock A print(self.name, ' Unlock A', time.ctime()) def run(self): self.a() self.b() if __name__ == '__main__': lockA = threading.Lock() lockB = threading.Lock() t_list = [] for i in range(5): t_list.append(MyThred()) for t in t_list: t.start() for t in t_list: t.join() # Example 2、 Recursive lock """ 1、 Recursive locks can be reused 2、 There is a timer inside , A lock . A lock can be locked many times (lock.acquire()), The number of locks is recorded internally , Unlock every time (lock.release()), Internal automatic reduction 1, The operation of locking and unlocking is aimed at a lock , So there will be no deadlock . 3、 In the following example, there is only one lock , Used by multiple threads . 4、 Try to use recursive locks when using locks , Avoid deadlocks """ class MyThred(threading.Thread): def a(self): lock.acquire() # Lock print(self.name, ' Lock ', time.ctime()) time.sleep(3) lock.acquire() # Lock print(self.name, ' Lock ', time.ctime()) lock.release() # Unlock print(self.name, ' Unlock ', time.ctime()) lock.release() # Unlock print(self.name, ' Unlock ', time.ctime()) def b(self): lock.acquire() # Lock print(self.name, ' Lock ', time.ctime()) time.sleep(3) lock.acquire() # Lock print(self.name, ' Lock ', time.ctime()) lock.release() # Unlock print(self.name, ' Unlock ', time.ctime()) lock.release() # Unlock print(self.name, ' Unlock ', time.ctime()) def run(self): self.a() self.b() if __name__ == '__main__': lock = threading.RLock() t_list = [] for i in range(5): t_list.append(MyThred()) for t in t_list: t.start() for t in t_list: t.join()