diff --git a/2022.03.16_frequently_used_python_package/threading_example.py b/2022.03.16_frequently_used_python_package/threading_example.py new file mode 100644 index 0000000..c465f55 --- /dev/null +++ b/2022.03.16_frequently_used_python_package/threading_example.py @@ -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.") \ No newline at end of file