Python多线程:主线程等待所有子线程结束

from time import ctime
import threading
import time

def a():
    #for i in range(5):
        print('Program a is running... at ', ctime(),u'.线程名为:',threading.current_thread().name )
        time.sleep(0.2)
        
def b(x):
    #for i in range(5):
        print('Program b('+x+') is running... at ', ctime(),u'.线程名为:',threading.current_thread().name )
        time.sleep(0.1)
        

if __name__ == '__main__':
    print('Mainthread %s is running...' % threading.current_thread().name)
    thread_list = []
    for i in range(400):#同时运行多个
       t1= threading.Thread(target=a)
       thread_list.append(t1)
       
    t2 = threading.Thread(target=b, args=('Python',))
    thread_list.append(t2)
    t3 = threading.Thread(target=b, args=('Java',))
    thread_list.append(t3)
    

    for t in thread_list:
        t.setDaemon(True)  # 设置为守护线程,不会因主线程结束而中断
        t.start()
    for t in thread_list:
        t.join()  # 子线程全部加入,主线程等所有子线程运行完毕

    print('Mainthread %s ended.' % threading.current_thread().name)
 

你可能感兴趣的:(Python多线程:主线程等待所有子线程结束)