測試代碼
import threading
import time
def do_thread_test():
print 'start thread time:', time.strftime('%H:%M:%S')
time.sleep(5)
print 'stop thread time:', time.strftime('%H:%M:%S')
threads = []
for i in range(2):
thread1 = threading.Thread(target=do_thread_test)
thread1.setDaemon(True)
threads.append(thread1)
for t in threads:
t.start()
for t in threads:
t.join()
print 'stop main thread', time.strftime('%H:%M:%S')
注釋掉join,注釋掉setDaemon(或設置為False),執行結果
start thread time: 21:57:40
start thread time: 21:57:40
stop main thread 21:57:40 主
stop thread time: 21:57:45
stop thread time: 21:57:45
注釋掉join,設置setDaemon=True,執行結果
start thread time: 21:59:47
start thread time: 21:59:47
stop main thread 21:59:47
join方法的作用是阻塞,等待子線程結束
join無參數,注釋掉setDaemon(或設置為True/False),執行結果
start thread time: 22:02:00
start thread time: 22:02:00
stop thread time: 22:02:05
stop thread time: 22:02:05
stop main thread 22:02:05 主
阻塞時間為time*thread_num
join參數為1,注釋掉setDaemon(或設置為False),執行結果
start thread time: 22:11:22
start thread time: 22:11:22
stop main thread 22:11:24
stop thread time: 22:11:27
stop thread time: 22:11:27
join參數為1,設置setDaemon=True,執行結果(阻塞time*thread_num)
start thread time: 22:12:38
start thread time: 22:12:38
stop main thread 22:12:40
原文鏈接:https://blog.csdn.net/weixin_44961798/article/details/108269495