zoukankan      html  css  js  c++  java
  • 多线程03

    # -*- 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) 开始线程执行
  • 相关阅读:
    第二类斯特林数学习笔记
    [ZJOI2017]树状数组
    「LibreOJ Round #6」花火
    [Ynoi2016]这是我自己的发明 莫队
    codeforces706E
    扩展CRT
    PKUSC2018游记
    「PKUWC 2018」Minimax
    「SHOI2015」(LOJ2038)超能粒子炮・改
    Codeforces712E
  • 原文地址:https://www.cnblogs.com/dengyg200891/p/4940766.html
Copyright © 2011-2022 走看看