from threading import Thread,Lock from time import sleep class Task1(Thread): """y';]lopppdocstring for Task1""" def run(self): while 1: if lock1.acquire(): print("---task1----") sleep(1) lock2.release() class Task2(Thread): """docstring for Task2""" def run(self): while 1: if lock2.acquire(): print("---task2----") sleep(1) lock3.release() class Task3(Thread): """docstring for Task3""" def run(self): while 1: if lock3.acquire(): print("---task3----") sleep(1) lock1.release() #创建3把锁 #使用lock创建的锁,默认没有“上锁” lock1 = Lock() lock2 = Lock() lock2.acquire() lock3 = Lock() lock3.acquire() t1 = Task1() t2 = Task2() t3 = Task3() t1.start() t2.start() t3.start()