1 import _thread 2 import time 3 def fun1(thread_name,delay): 4 print('開始運行fun1,線程的名:',thread_name) 5 time. sleep(delay) 6 print('運行fun1結束') 7 8 def fun2(thread_name,delay): 9 print('開始運行fun2,線程的名:',thread_name) 10 time.sleep(delay) 11 print('運行fun2結束') 12 if __name__ == '__main__': 13 print('開始運行') 14 #創建線程 15 _thread.start_new_thread(fun1,('thread-1',3)) 16 time.sleep(2) 17 _thread.start_new_thread(fun2,('thread-2',3)) 18 time.sleep(7)
1 開始運行 2 開始運行fun1,線程的名: thread-1 3 開始運行fun2,線程的名: thread-2 4 運行fun1結束 5 運行fun2結束