初识Python多线程中的setDaemon与join

正常多线程代码

import threading
import time

def runThread1():
    time1 = time.time()
    print("runThread1 starting")
    time.sleep(8)
    print("runThread1 ending")
    time2 = time.time()
    print("runThread1 time : ",(time2 - time1)) # 计算程序运行时间,理论值8
       
def runThread2():
    flag = 3
    time1 = time.time()
    print("runThread2 starting")
    while flag:
        print("Hello")
        flag -= 1
        time.sleep(2)
    print("runThread2 ending")
    time2 = time.time()
    print("runThread2 time : ",(time2 - time1)) # 计算程序运行时间,理论值6
   
if __name__ == '__main__':
    runThread1 = threading.Thread(target = runThread1)
    runThread2 = threading.Thread(target = runThread2)
    
    runThread1.start()
    runThread2.start()

    print("main end")
运行结果
runThread1 starting
runThread2 starting
main end
Hello
Hello
Hello
runThread2 ending
runThread2 time :  6.001581907272339
runThread1 ending
runThread1 time :  8.001220941543579
结果分析

两个线程正常运行并正常结束,main end 在线程执行时正常输出


setDaemon 测试

import threading
import time

def runThread1():
    time1 = time.time()
    print("runThread1 starting")
    time.sleep(8)
    print("runThread1 ending")
    time2 = time.time()
    print("runThread1 time : ",(time2 - time1)) # 计算程序运行时间,理论值8
       
def runThread2():
    flag = 3
    time1 = time.time()
    print("runThread2 starting")
    while flag:
        print("Hello")
        flag -= 1
        time.sleep(2)
    print("runThread2 ending")
    time2 = time.time()
    print("runThread2 time : ",(time2 - time1)) # 计算程序运行时间,理论值6
   
if __name__ == '__main__':
    runThread1 = threading.Thread(target = runThread1)
    runThread2 = threading.Thread(target = runThread2)
    runThread1.setDaemon(True)
    
    runThread1.start()
    runThread2.start()
    
    print("main end")
运行结果
runThread1 starting
runThread2 starting
main end
Hello
Hello
Hello
runThread2 ending
runThread2 time :  6.00260591506958
结果分析

Thread2本该运行8秒后结束,但通过设置setDaemon方法设置后,在Thread1运行6秒并结束后Thread2也立刻随之结束。原因是Thread2在Main线程(__main__所在线程)中创建,Thread2调用setDaemon(True)后,即将Main线程设置为Thread2的守护线程,即当Mian线程结束时无论Thread2有没有结束都会强制结束,而因为Main线程不是Thread1的守护线程,所以Main线程等待Thread1结束后才结束。


setDeamon 测试2

import threading
import time

def runThread1():
    time1 = time.time()
    print("runThread1 starting")
    time.sleep(8)
    print("runThread1 ending")
    time2 = time.time()
    print("runThread1 time : ",(time2 - time1)) # 计算程序运行时间,理论值8
       
def runThread2():
    flag = 3
    time1 = time.time()
    print("runThread2 starting")
    while flag:
        print("Hello")
        flag -= 1
        time.sleep(2)
    print("runThread2 ending")
    time2 = time.time()
    print("runThread2 time : ",(time2 - time1)) # 计算程序运行时间,理论值6
   
if __name__ == '__main__':
    runThread1 = threading.Thread(target = runThread1)
    runThread2 = threading.Thread(target = runThread2)
    runThread1.setDaemon(True)
    runThread2.setDaemon(True)
    runThread2.start() # 6
    runThread1.start() # 8
    
    print("main end")
运行结果
runThread2 starting
Hello
runThread1 starting
main end
结果分析

将Thread1、Thread2都设置setDaemon(True)后,在主程序输出完main end后结束的时候也都被强行结束。


join测试

import threading
import time

def runThread1():
    time1 = time.time()
    print("runThread1 starting")
    time.sleep(8)
    print("runThread1 ending")
    time2 = time.time()
    print("runThread1 time : ",(time2 - time1)) # 计算程序运行时间,理论值8
       
def runThread2():
    flag = 3
    time1 = time.time()
    print("runThread2 starting")
    while flag:
        print("Hello")
        flag -= 1
        time.sleep(2)
    print("runThread2 ending")
    time2 = time.time()
    print("runThread2 time : ",(time2 - time1)) # 计算程序运行时间,理论值6
   
if __name__ == '__main__':
    runThread1 = threading.Thread(target = runThread1)
    runThread2 = threading.Thread(target = runThread2)
    #runThread1.setDaemon(True)
    
    runThread1.start()
    runThread2.start()
    
    runThread2.join()
    
    print("main end")
运行结果
runThread1 starting
runThread2 starting
Hello
Hello
Hello
runThread2 ending
runThread2 time :  6.001340389251709
main end
runThread1 ending
runThread1 time :  8.000601291656494

结果分析

main end 在Thread2运行完成后才输出,说明Thread2.join()表示等待Thread2线程结束,未结束的话一直在这里等待,线程结束后才执行后续的语句


setDaemon & join

import threading
import time

def runThread1():
    time1 = time.time()
    print("runThread1 starting")
    time.sleep(8)
    print("runThread1 ending")
    time2 = time.time()
    print("runThread1 time : ",(time2 - time1)) # 计算程序运行时间,理论值8
       
def runThread2():
    flag = 3
    time1 = time.time()
    print("runThread2 starting")
    while flag:
        print("Hello")
        flag -= 1
        time.sleep(2)
    print("runThread2 ending")
    time2 = time.time()
    print("runThread2 time : ",(time2 - time1)) # 计算程序运行时间,理论值6
   
if __name__ == '__main__':
    runThread1 = threading.Thread(target = runThread1)
    runThread2 = threading.Thread(target = runThread2)
    runThread1.setDaemon(True)
    runThread2.setDaemon(True)
    runThread2.start() # 6
    
    runThread1.start() # 8
    
    runThread2.join() 
    
    print("main end")
运行结果
runThread2 starting
Hello
runThread1 starting
Hello
Hello
runThread2 ending
runThread2 time :  6.003632545471191
main end
结果分析

Main线程被设置为Thread1的守护线程,Main线程在Thread2结束并输出main end后结束,Thread1也被强行结束

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值