import time
import threading
def text():
while True:
print(f"Hello from {threading.current_thread().name}")
time.sleep(2)
if __name__ == "__main__":
#创建线程
thread0 = threading.Thread(target=text, name="Thread-0")
thread1 = threading.Thread(target=text, name="Thread-1")
#启动线程
thread0.start()
time.sleep(1)
thread1.start()
#阻塞主线程,等待子线程都完成
thread0.join()
thread1.join()
python多线程
最新推荐文章于 2024-10-02 10:53:34 发布