11 同步
1.堵塞:是否等待
2. 什么是同步?按顺序执行
同步就是协同步调,按预定的先后次序进行运行。如:你说完,我再说。
3.同步:多个线程有序执行
from threading import Thread,Lock from time import sleep class Task1(Thread): def run(self): while True: if lock1.acquire(): print("------Task 1 -----") sleep(0.5) lock2.release() class Task2(Thread): def run(self): while True: if lock2.acquire(): print("------Task 2 -----") sleep(0.5) lock3.release() class Task3(Thread): def run(self): while True: if lock3.acquire(): print("------Task 3 -----") sleep(0.5) lock1.release() #使用Lock创建出的锁默认没有“锁上” lock1 = Lock() #创建另外一把锁,并且“锁上” lock2 = Lock() lock2.acquire() #创建另外一把锁,并且“锁上” lock3 = Lock() lock3.acquire() t1 = Task1() t2 = Task2() t3 = Task3() t1.start() t2.start() t3.start()