当前第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闭包的简单介绍(附示例)
趣味玩转——用Python分析《三国演义》中的社交网络
Python怎么新建文件夹
Python如何遍历文件夹
为什么 1000000000000000 in range(1000000000000001) 在 Python3 里速度那么快
Python cookbook(字符串与文本)针对任意多的分隔符拆分字符串操作
Python获取当前时间
怎么查看Python的安装目录
Python动态定义函数的方法介绍
更多相关阅读请进入《Python》频道 >>
人民邮电出版社
python入门书籍,非常畅销,超高好评,python官方公认好书。
转载请注明出处:木庄网络博客 » python多线程之事件Event的使用详解