join()阻塞的用法,用来检测线程有没有完全执行完毕

#!/usr/bin/env python
#-*- coding:utf-8 -*-
import threading
import time

def sayHi(n):
  time.sleep(1)
  print 'Hi this is thread %s' %n

res_list = []
for i in range(20):
  res_list.append(threading.Thread(target = sayHi, args = (i,)))

for i in res_list:
  i.start()

#join阻塞的用法,用来检测线程完全执行完毕,如果线程没有执行完,会一直堵塞在这里,不会执行print ‘the last line!’
for i in res_list:
  i.join()

print 'the last line!'

04-19 13:46