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