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 在线程执行时正常输出
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结束后才结束。
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后结束的时候也都被强行结束。
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线程结束,未结束的话一直在这里等待,线程结束后才执行后续的语句
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也被强行结束