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

    # -*- coding: utf-8 -*-
    #python 27
    #xiaodeng
    #线程和进程
    #http://www.cnblogs.com/fnng/p/3691053.html
    
    
    
    #多线程:
    from time import sleep,ctime
    import threading
    
    def move(func):
        for i in range(2):
            print 'Start playing:%s.%s'%(func,ctime())
            sleep(5)
    
    def music(func):
        for i in range(2):
            print 'Start playing:%s.%s'%(func,ctime())
            sleep(2)
    
    def player(name): 
        r=name.split('.')[1]
    
        if r=='mp3':
            music(name)
        else:
            if r=='mp4':
                move(name)
            else:
                print 'error:the format is not recognized!'
    
    
    list=[u'爱情买卖.mp3',u'大秦帝国.mp4']
    threads=[]
    files=range(len(list))
    
    
    #创建线程
    for i in files:
        t=threading.Thread(target=player,args=(list[i],))
        threads.append(t)
    
    
    if __name__=='__main__':
        #启动线程
        for i in files:
            threads[i].start()
        #用于等待线程终止,
        #作用:在子线程完成运行之前,这个子线程的父线程将一直阻塞。
        for i in files:
            threads[i].join()
    
        #主线程
        print 'end:%s'%(ctime())
  • 相关阅读:
    C#深复制和浅复制
    C#程序设计六大原则记录
    C#异步
    线程同步
    线程基础
    委托,事件
    XmlSerializer
    C#接口
    C#封装
    C#多态
  • 原文地址:https://www.cnblogs.com/dengyg200891/p/4940631.html
Copyright © 2011-2022 走看看