rgfergrg
This commit is contained in:
26
test111.py
Normal file
26
test111.py
Normal file
@@ -0,0 +1,26 @@
|
||||
import threading
|
||||
import time
|
||||
|
||||
def daemon_thread_function():
|
||||
print("守护线程开始")
|
||||
time.sleep(5)
|
||||
print("守护线程结束")
|
||||
|
||||
def non_daemon_thread_function():
|
||||
print("非守护线程开始")
|
||||
time.sleep(2)
|
||||
print("非守护线程结束")
|
||||
|
||||
# 创建守护线程
|
||||
daemon_thread = threading.Thread(target=daemon_thread_function, daemon=True)
|
||||
# 创建非守护线程
|
||||
non_daemon_thread = threading.Thread(target=non_daemon_thread_function)
|
||||
|
||||
# 启动线程
|
||||
daemon_thread.start()
|
||||
non_daemon_thread.start()
|
||||
|
||||
print("主线程继续执行")
|
||||
# 主线程等待非守护线程结束
|
||||
non_daemon_thread.join()
|
||||
print("主线程结束")
|
||||
Reference in New Issue
Block a user