pyhon-- thread synchronization condition (event)

event.wait () when the wait is not set, the thread is stuck, do not go, once set, is equivalent to the pass

event.set () is set to the wait

event.clear () to clear the set 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()
        
    
    
    
    
    
    
    
    
    
        

Guess you like

Origin www.cnblogs.com/hyxk/p/11287738.html