Python 线程(五):Event

Event:  是线程同步的一种方式,类似于一个标志,当该标志为false时,所有等待该标志的线程阻塞,当为true时,所有等待该标志的线程被唤醒

 

isSet():       当内置标志为True时返回True。 
set():         将标志设为True,并通知所有处于等待阻塞状态的线程恢复运行状态。 
clear():       将标志设为False。 
wait([timeout]):  如果标志为True将立即返回,否则阻塞线程至等待阻塞状态,等待其他线程调用set()

 1 import threading
 2 import time
 3 
 4 event = threading.Event()
 5 
 6 def func():
 7     print "%s is waiting for event...\n" % threading.currentThread().getName()
 8     event.wait()
 9 
10     print "%s get the Event..\n" % threading.currentThread().getName()
11 
12 
13 if __name__ == "__main__":
14     t1 = threading.Thread(target=func)
15     t2 = threading.Thread(target=func)
16 
17     t1.start()
18     t2.start()
19 
20     time.sleep(2)
21 
22     print "MainThread set Event\n"
23 
24     event.set()
25 
26     t1.join()
27     t2.join()

 

posted @ 2014-03-05 11:05  Fly Hawk  阅读(1000)  评论(0编辑  收藏  举报