当前第2页 返回上一页
场景:当小伙伴a,b,c集结完毕后,请客的人发话:开吃咯!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | import threading
import time
event = threading.Event()
def chiHuoGuo(name):
print '%s 已经启动' % threading.currentThread().getName()
print '小伙伴 %s 已经进入就餐状态!' % name
time.sleep( 1 )
event.wait()
print '%s 收到通知了.' % threading.currentThread().getName()
print '%s 小伙伴 %s 开始吃咯!' % (time.time(), name)
class myThread (threading.Thread):
def __init__( self , name):
threading.Thread.__init__( self )
self .people = name
def run( self ):
chiHuoGuo( self .people)
print ( "qq交流群:226296743" )
print ( "结束线程: %s" % threading.currentThread().getName())
threads = []
thread1 = myThread( "a" )
thread2 = myThread( "b" )
thread3 = myThread( "c" )
threads.append(thread1)
threads.append(thread2)
threads.append(thread3)
for thread in threads:
thread.start()
time.sleep( 0.1 )
print '集合完毕,人员到齐了,开吃咯!'
event. set ()
|
运行结果:
Thread-1 已经启动
小伙伴 a 已经进入就餐状态!
Thread-2 已经启动
小伙伴 b 已经进入就餐状态!
Thread-3 已经启动
小伙伴 c 已经进入就餐状态!
集合完毕,人员到齐了,开吃咯!
Thread-1 收到通知了.
1516780957.47 小伙伴 a 开始吃咯!
qq交流群:226296743
结束线程: Thread-1
Thread-3 收到通知了.
1516780957.47 小伙伴 c 开始吃咯!Thread-2 收到通知了.
qq交流群:226296743
1516780957.47 小伙伴 b 开始吃咯!结束线程: Thread-3
qq交流群:226296743
结束线程: Thread-2
相关推荐:
python线程池threadpool的实现
以上就是python多线程之事件Event的使用详解的详细内容,更多文章请关注木庄网络博客!!
返回前面的内容
相关阅读 >>
如何完整写一个爬虫框架
Python不支持的数据类型有哪些
Python中的mkdir方法怎么用
关于Python操作文件方法的总结(收藏)
Python matplotlib中文显示参数设置解析_Python
Python实现求一个集合所有子集的示例
基于tpc-c基准的Python orm的性能测试详解
Python取余运算符是什么?
Python中闭包的简单介绍(附示例)
Python可以开发安卓app吗
更多相关阅读请进入《Python》频道 >>
人民邮电出版社
python入门书籍,非常畅销,超高好评,python官方公认好书。
转载请注明出处:木庄网络博客 » python多线程之事件Event的使用详解