Create threading_example.py

This commit is contained in:
guanjihuan 2023-11-17 02:26:19 +08:00
parent f5165221c4
commit 4e7244ddf3

View File

@ -0,0 +1,21 @@
import threading
import time
def print_numbers(x):
for i in range(5):
time.sleep(1)
print(f"Thread {x}: {i}")
# 创建两个线程
thread1 = threading.Thread(target=print_numbers, args=('A'))
thread2 = threading.Thread(target=print_numbers, args=('B'))
# 启动线程
thread1.start()
thread2.start()
# 等待两个线程完成
thread1.join()
thread2.join()
print("Both threads have finished.")