This commit is contained in:
2024-03-29 12:31:49 +08:00
parent 6637dbdd36
commit a58118d3fb
7 changed files with 89 additions and 30 deletions

View File

@@ -6,16 +6,23 @@ def print_numbers(x):
time.sleep(1)
print(f"Thread {x}: {i}")
# 串行测试
start_time = time.time()
print_numbers('A')
print_numbers('B')
end_time = time.time()
print(end_time - start_time, '\n')
# 创建两个线程
thread1 = threading.Thread(target=print_numbers, args=('A'))
thread2 = threading.Thread(target=print_numbers, args=('B'))
start_time = time.time()
# 启动线程
thread1.start()
thread2.start()
# 等待两个线程完成
thread1.join()
thread2.join()
print("Both threads have finished.")
end_time = time.time()
print(end_time - start_time)