首页 > 编程语言 > 详细

多线程03

时间:2015-11-05 22:07:27      阅读:284      评论:0      收藏:0      [点我收藏+]
# -*- coding: utf-8 -*-
#python 27
#xiaodeng
#线程和进程
#http://www.cnblogs.com/fnng/p/3691053.html
#http://python.jobbole.com/81546/
#http://www.runoob.com/python/python-multithreading.html



#多线程:
from time import sleep,ctime
import threading

def super_player(file,time):
    #for i in range(2):
    print Start playing:%s.%s%(file,ctime())
    sleep(time)


#播放的文件与播放时长
list={u爱情买卖.mp3:3,u大秦帝国.mp4:5,u我和你.mp3:4}
threads=[]
files=range(len(list))#[0, 1, 2]



#创建线程
for file,time in list.items():#字典的items()方法
    #print file,time
    t=threading.Thread(target=super_player,args=(file,time))#print file,time
    #print t
    ‘‘‘
    <Thread(Thread-1, initial)>
    <Thread(Thread-2, initial)>
    <Thread(Thread-3, initial)>
    ‘‘‘
    threads.append(t)


if __name__==__main__:
    #启动线程
    for i in files:
        #print i#0,1,2,请注意print t的打印结果。
        threads[i].start()
    #join()
    #调用Thread.join()将会使主调线程阻塞,等待线程终止。
    for i in files:
        threads[i].join()

    #主线程
    print End:%s%ctime()



#方法
#threads[i].getName()#返回线程的名字
#join(timeout=None) 程序挂起,直到线程结束,如果给出timeout,则最多阻塞timeout秒
#start(self) 开始线程执行

 

多线程03

原文:http://www.cnblogs.com/dengyg200891/p/4940766.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!