import threading
from time import ctime,sleep
def music(func):
for i in range(3):
print "I was listening to %s. %s" %(func,ctime())
sleep(5)
def move(func):
for i in range(5):
print "I was at the %s! %s" %(func,ctime())
sleep(5)
threads = []
t1 = threading.Thread(target=music,args=(u'爱情买卖',))
threads.append(t1)
t2 = threading.Thread(target=move,args=(u'阿凡达',))
threads.append(t2)
if __name__ == '__main__':
for t in threads:
#t.setDaemon(True)
t.start()
t.join()
print "all over %s" %ctime()
C:Python27python.exe C:/Users/TLCB/PycharmProjects/untitled/mycompany/thread/p1.py
I was listening to 爱情买卖. Thu Aug 24 16:49:52 2017
I was at the 阿凡达! Thu Aug 24 16:49:52 2017
I was listening to 爱情买卖. Thu Aug 24 16:49:57 2017
I was at the 阿凡达! Thu Aug 24 16:49:57 2017
I was at the 阿凡达! Thu Aug 24 16:50:02 2017I was listening to 爱情买卖. Thu Aug 24 16:50:02 2017
I was at the 阿凡达! Thu Aug 24 16:50:07 2017
I was at the 阿凡达! Thu Aug 24 16:50:12 2017
all over Thu Aug 24 16:50:17 2017
Process finished with exit code 0
#coding=utf-8
import threading
from time import ctime,sleep
def music(func):
for i in range(3):
print "I was listening to %s. %s" %(func,ctime())
sleep(10)
def move(func):
for i in range(5):
print "I was at the %s! %s" %(func,ctime())
sleep(5)
threads = []
t1 = threading.Thread(target=music,args=(u'爱情买卖',))
threads.append(t1)
t2 = threading.Thread(target=move,args=(u'阿凡达',))
threads.append(t2)
if __name__ == '__main__':
for t in threads:
#t.setDaemon(True)
t.start()
t.join(5)
print "all over %s" %ctime()
C:Python27python.exe C:/Users/TLCB/PycharmProjects/untitled/mycompany/thread/p3.py
I was listening to 爱情买卖. Thu Aug 24 15:45:47 2017
I was at the 阿凡达! Thu Aug 24 15:45:47 2017
I was at the 阿凡达! Thu Aug 24 15:45:52 2017
all over Thu Aug 24 15:45:52 2017
I was listening to 爱情买卖. Thu Aug 24 15:45:57 2017
I was at the 阿凡达! Thu Aug 24 15:45:57 2017
I was at the 阿凡达! Thu Aug 24 15:46:02 2017
I was listening to 爱情买卖. Thu Aug 24 15:46:07 2017
I was at the 阿凡达! Thu Aug 24 15:46:07 2017
Process finished with exit code 0
join([timeout])
等待知道线程终止,这个堵塞调用线程直到线程join()方法是被调用终止。
正常或者通过一个未知异常--或者直到可选的超时发生
当超时超时是存在且不为空,它应该是一个数值 单位是秒。
join()总是返回None,你必须调用isAlive()在join()来决定是否一个timeout 发生