您的位置:首页 > 编程语言 > Python开发

Python多线程join的用法

2016-04-21 22:10 691 查看
import threading, time
def Myjoin():
print 'hello world!'
time.sleep(1)
for i in range(5):
t=threading.Thread(target=Myjoin)
t.start()
t.join()
print 'hello main'
#输出:(每隔一秒输出)
hello world!
hello world!
hello world!
hello world!
hello world!
hello main
注释掉join
#输出:
hello world!
hello world!hello world!

hello world!
hello world!hello main
(全部输出后等待一秒程序结束)


所以join的作用是保证当前线程执行完成后,再执行其它线程。join可以有timeout参数,表示阻塞其它线程timeout秒后,不再阻塞。详见官方文档。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: