标签:for ram __name__ style star join() port nbsp end
1 from time import ctime 2 import threading 3 4 def coding(language): 5 for i in range(5): 6 print(‘I\‘m coding ‘,language, ‘ program at ‘, ctime() ) 7 8 def music(): 9 for i in range(5): 10 print(‘I\‘m listening music at ‘, ctime()) 11 12 if __name__ == ‘__main__‘: 13 14 print(‘thread %s is running...‘ % threading.current_thread().name) 15 16 thread_list = [] 17 t1 = threading.Thread(target=coding, args=(‘Python‘,)) 18 t2 = threading.Thread(target=music) 19 thread_list.append(t1) 20 thread_list.append(t2) 21 22 for t in thread_list: 23 t.setDaemon(True) # 设置为守护线程 24 t.start() 25 t.join() # 在这个子线程完成运行之前,主线程将一直被阻塞 26 27 print(‘thread %s ended.‘ % threading.current_thread().name)
标签:for ram __name__ style star join() port nbsp end
原文地址:https://www.cnblogs.com/LyShark/p/9066585.html