import time import threading def music(): for i in range(3): print("聽音樂。。。") time.sleep(0.5) def coding(): for i in range(3): print("敲代碼。。。") time.sleep(0.5) if __name__ == '__main__': start = time.time() music_thread = threading.Thread(target=music) coding_thread = threading.Thread(target=coding) music_thread.start() coding_thread.start() music_thread.join() coding_thread.join() end = time.time() print(f'程序執行了{end - start}秒')
結果如下:無序
聽音樂。。。 敲代碼。。。 聽音樂。。。敲代碼。。。 敲代碼。。。 聽音樂。。。 程序執行了1.5029683113098145秒