Python多线程join的用法

 1 import threading, time
 2 def Myjoin():
 3     print 'hello world!'
 4     time.sleep(1)
 5 for i in range(5):
 6     t=threading.Thread(target=Myjoin)
 7     t.start()
 8     t.join()
 9 print 'hello main'
10 #输出:(每隔一秒输出)
11 hello world!
12 hello world!
13 hello world!
14 hello world!
15 hello world!
16 hello main
17 注释掉join
18 #输出:
19 hello world!
20  hello world!hello world!
21 
22 hello world!
23 hello world!hello main
24 (全部输出后等待一秒程序结束)

所以join的作用是保证当前线程执行完成后,再执行其它线程。join可以有timeout参数,表示阻塞其它线程timeout秒后,不再阻塞。详见官方文档。

转载于:https://www.cnblogs.com/dream-for/p/5419051.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值