多个线程通过互斥锁有序的执行
作者:互联网
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()
标签:Thread,lock3,Lock,acquire,lock2,互斥,线程,有序,sleep 来源: https://www.cnblogs.com/qiushanjian/p/10341230.html