码迷,mamicode.com
首页 > 编程语言 > 详细

创建多线程类

时间:2015-11-05 23:48:11      阅读:241      评论:0      收藏:0      [点我收藏+]

标签:

# -*- coding: cp936 -*-
#python 27
#xiaodeng
#http://www.cnblogs.com/fnng/p/3489321.html
#创建多线程类


import threading
from time import sleep,ctime


class MyThread(threading.Thread):#threading.Thread
    创建MyThread类,用于继承threading.Thread类.
    def __init__(self,func,args,name=‘‘):
        使用类的初始化方法对func、args、name等参数进行初始化.
        threading.Thread.__init__(self)
        self.func=func
        self.args=args
        self.name=name

    def run(self):
        apply(self.func,self.args)

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


dict={u爱情买卖.mp3:3,u大秦帝国.mp4:5}


#创建线程
threads=[]
files=range(len(dict))


for k,v in dict.items():
    print super_play.__name__#super_play
    #MyThread(super_play,(k,v),super_play.__name__)
    t=MyThread(super_play,(k,v),super_play.__name__)
    threads.append(t)


if __name__==__main__:
    #启动线程
    for i in files:
        threads[i].start()
    for i in files:
        threads[i].join()
    #主线程
    print End:%s%ctime()

 

创建多线程类

标签:

原文地址:http://www.cnblogs.com/dengyg200891/p/4940995.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!