event.wait() wait未被设定时,线程会被卡住,执行不下去,一旦设定,就相当于pass
event.set() 来给wait设定
event.clear() 来清除set设定
import threading
import time
class Boss(threading.Thread):
def run(self):
print("boss: 今晚加班到12.00")
print(event.isSet())
event.set()
time.sleep(5)
print("时间到了,下班了")
print(event.isSet())
event.set()
class Worker(threading.Thread):
def run(self):
event.wait()
print("Worker:惨 啊")
time.sleep(1)
event.clear()
event.wait()
print("yes")
if __name__ == '__main__':
event = threading.Event()
threads = []
for i in range(5):
threads.append(Worker())
threads.append(Boss())
for t in threads:
t.start()
for t in threads:
t.join()
原文地址:https://www.cnblogs.com/hyxk/p/11287738.html
时间: 2024-11-04 09:15:23