code

"""
守护线程:一个子线程,会随着主线程的退出直接退出
    不论子线程中是否执行完成
    不离不弃,死生相随
"""
import threading
import time


def test():
    while True:
        print(f"{threading.current_thread().getName()}线程"
              f"正在执行中....")
        time.sleep(1)
        print(threading.enumerate())
        print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")


if __name__ == "__main__":
    # 创建一个子线程
    t = threading.Thread(target=test)
    t.name = "正在跟家兴聊天中"
    t.daemon = True
    # t.setDaemon(True)  # 设置t线程为主线程的守护线程
    t.start()

    time.sleep(2)
    print("主线程:QQ软件")
    # 这句话执行完成,表示主线程中没有代码了~

 

 

 

 

 

 

 

 

 

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-12-26
  • 2021-04-08
猜你喜欢
  • 2021-07-27
  • 2021-12-20
  • 2022-12-23
  • 2021-11-22
  • 2021-08-18
  • 2022-12-23
相关资源
相似解决方案