先占個坑,后續進行更新...
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'愛情買賣',))
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)將線程聲明為守護線程,必須在start()
方法調用之前設置,如果不設置為守護線程程序會被無限掛起。
子線程啟動后,父線程也繼續執行下去,當父線程執行完最后
一條語句print "all over %s" %ctime()后,沒有等待子
線程,直接就退出了,同時子線程也一同結束。
"""
t.start()
t.join()
"""
join()的作用是,在子線程完成運行之前,這個子線程的父線程將一直被阻塞。
"""
print("its time to sleep %s"% time.ctime())
---
i like girls 愛情買賣 Mon Oct 9 23:00:15 2017
i like run奔跑吧兄弟 Mon Oct 9 23:00:15 2017
i like girls 愛情買賣 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