synchronization object event

'''
event.wait() #Set blocking, the thread will be blocked at this time, and when it encounters event.set(), the thread will continue to run
event.set()
event.clear()  #
'''

import time,threading

class Boss(threading.Thread):
     def run(self):
         print ( ' Boss: ' , ' Work overtime until 10 o'clock tonight ' )
        time.sleep(1)
        event.set()
        time.sleep( 8 )
         print ( ' Boss: ' , ' It's off work now ' )
        event.set()

class Worker(threading.Thread):
    def run(self):
        event.wait()
        print ( self.name , ' Ahhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhht
        time.sleep(4)
        event.clear()
        event.wait()
        print (self.name, ' Oye!!!!!!! ' )
        event.clear()


if __name__ == '__main__':

    event = threading.Event()

    l = []
    for i in range(5):
        l.append(Worker())
    l.append(Boss())

    for i in l:
        i.start()

    for i in l:
        i.join()
    print('ending--------------')

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325348777&siteId=291194637