#-*-coding:utf-8-*-
'''
Created on 2016年9月17日
@author: zroad
'''
import threading
from time import ctime,sleep
def music(func):
for i in range(2):
print "I was listening to %s. %s" %(func,ctime())
sleep(1)
def move(func):
for i in range(2):
print "I was at the %s! %s" %(func,ctime())
sleep(5)
threads = []
#创建线程t1,使用threading.Thread()方法,在这个方法中调用music方法
#target=music,args方法对music进行传参。
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()
python多线程
最新推荐文章于 2024-03-28 14:05:33 发布