先占個(gè)坑,后續(xù)進(jìn)行更新...
import threading, time
a = ["maatjing", "leetcode", "hsjfans", "dreamweaver"]
def music(fun):
for i in range(2):
print("i like girls %s %s" % (fun, time.ctime()))
time.sleep(1)
def run(fun):
for i in range(2):
print("i like run%s %s" % (fun, time.ctime()))
time.sleep(4)
treads=[]
t1 = threading.Thread(target=music, args=(u'愛(ài)情買(mǎi)賣(mài)',))
treads.append(t1)
t2 = threading.Thread(target=run, args=(u'奔跑吧兄弟',))
treads.append(t2)
if __name__ == '__main__':
for t in treads:
t.setDaemon(True)
"""
setDaemon()setDaemon(True)將線程聲明為守護(hù)線程斋荞,必須在start()
方法調(diào)用之前設(shè)置廷粒,如果不設(shè)置為守護(hù)線程程序會(huì)被無(wú)限掛起。
子線程啟動(dòng)后肄梨,父線程也繼續(xù)執(zhí)行下去,當(dāng)父線程執(zhí)行完最后
一條語(yǔ)句print "all over %s" %ctime()后,沒(méi)有等待子
線程枫弟,直接就退出了,同時(shí)子線程也一同結(jié)束鹏往。
"""
t.start()
t.join()
"""
join()的作用是淡诗,在子線程完成運(yùn)行之前,這個(gè)子線程的父線程將一直被阻塞掸犬。
"""
print("its time to sleep %s"% time.ctime())
---
i like girls 愛(ài)情買(mǎi)賣(mài) Mon Oct 9 23:00:15 2017
i like run奔跑吧兄弟 Mon Oct 9 23:00:15 2017
i like girls 愛(ài)情買(mǎi)賣(mài) Mon Oct 9 23:00:16 2017
i like run奔跑吧兄弟 Mon Oct 9 23:00:19 2017
its time to sleep Mon Oct 9 23:00:23 2017