python中的thread_Python中的thread
測(cè)試代碼
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(或設(shè)置為False),執(zhí)行結(jié)果
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,設(shè)置setDaemon=True,執(zhí)行結(jié)果
start thread time: 21:59:47
start thread time: 21:59:47
stop main thread 21:59:47
join方法的作用是阻塞,等待子線程結(jié)束
join無(wú)參數(shù),注釋掉setDaemon(或設(shè)置為True/False),執(zhí)行結(jié)果
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 主
阻塞時(shí)間為time*thread_num
join參數(shù)為1,注釋掉setDaemon(或設(shè)置為False),執(zhí)行結(jié)果
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參數(shù)為1,設(shè)置setDaemon=True,執(zhí)行結(jié)果(阻塞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
總結(jié)
以上是生活随笔為你收集整理的python中的thread_Python中的thread的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 女人梦到鞋子开胶好不好
- 下一篇: 梦到梦中梦意味着什么