多線程執行,使用join函數獲取全部線程執行完成時間 __author__ = "Alex Li" import threading import time def run(n): print("task ",n ) time.sleep(2) print("task done",n) start_time = time.time() t_objs = [] #存線程實例 for i in range(50): t = threading.Thread(target=run,args=("t-%s" %i ,)) t.start() t_objs.append(t) #爲了避免阻塞後面線程的啓動,不在這裏join,先放到一個列表裏 for t in t_objs: #循環線程實例列表,等待全部線程執行完畢 t.join() #等待子線程執行完成後,在執行主線程 print("----------all threads has finished...") print("cost:",time.time() - start_time)