先占个坑,后续进行更新...
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