zoukankan      html  css  js  c++  java
  • python 多线程join()

    #coding=utf-8
    import threading
    from time import ctime,sleep
    
    
    def music(func):
        for i in range(5):
            print "I was listening to %s. %s" %(func,ctime())
            print threading.currentThread()
            sleep(2)
    
    def move(func):
        for i in range(2):
            print "I was at the %s! %s" %(func,ctime())
            print threading.currentThread()
            sleep(50)
    
    threads = []
    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()
    
    
    我们只对上面的程序加了个join()方法,用于等待线程终止。join()的作用是,在子线程完成运行之前,这个子线程的父线程将一直被阻塞。
    
      注意:  join()方法的位置是在for循环外的,也就是说必须等待for循环里的两个进程都结束后,才去执行主进程。
    

  • 相关阅读:
    头文件<stdarg.h>
    头文件<signal.h>
    头文件<setjmp.h>
    头文件<math.h>
    头文件<locale.h>
    头文件<limits.h>
    头文件<ctype.h>
    头文件<assert.h>
    PHP error_reporting
    八大排序算法
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13349548.html
Copyright © 2011-2022 走看看