一、join 方法的主要作用是阻塞, 阻塞的对象是谁?
答:阻塞的对象是主线程,可以达到控制主线程什么时候结束的作用。
1、没有join
此时主线程和子线程相当于齐头并进,主线程不会等待子线程执行完了再结束,即不会阻塞
实验代码:
# coding: utf-8
# 测试多线程中join的功能
import threading, time
def doWaiting():
print('start waiting1: ' + time.strftime('%H:%M:%S') + "\n" )
time.sleep(3)
print('stop waiting1: ' + time.strftime('%H:%M:%S') + "\n")
def doWaiting1():
print('start waiting2: ' + time.strftime('%H:%M:%S') + "\n")
time.sleep(8)
print('stop waiting2: ', time.strftime('%H:%M:%S') + "\n")
if __name__ == '__main__':
tsk = []
thread1 = threading.Thread(target = doWaiting)
thread1.start()
tsk.append(thread1)
thread2 = threading.Thread(target = doWaiting1)
thread2.start()
tsk.append(thread2)
# print('start join: ' + time.strftime('%H:%M:%S') + "\n")
# for tt in tsk:
# tt.join()
print('end main thread: ' + time.strftime('%H:%M:%S') + "\n")
执行结果及分析:
2、有join
当主线程内的子线程都start后,主线程会阻塞并等待子线程执行完才继续执行,直观的表现是:子线程都start后,主线程若还有未执行的代码,此时不会继续执行剩余代码,而是等待子线程都执行完后再执行
if __name__ == '__main__':
tsk = []
thread1 = threading.Thread(target = doWaiting)
thread1.start()
tsk.append(thread1)
thread2 = threading.Thread(target = doWaiting1)
thread2.start()
tsk.append(thread2)
print('start join: ' + time.strftime('%H:%M:%S') + "\n")
for tt in tsk:
tt.join()
print('end main thread: ' + time.strftime('%H:%M:%S') + "\n")
执行结果及分析:
3、有join且传入阻塞时间time
主线程最长阻塞时间: 子线程数*join传入参数time,当每个子线程都执行了time时间甚至更长时,主线程才有可能达到这么长的阻塞时间。
注意:即使是主线程结束阻塞后执行完剩余代码,子线程也会继续执行,而不会随着主线程的结束而终止。
if __name__ == '__main__':
tsk = []
thread1 = threading.Thread(target = doWaiting)
thread1.start()
tsk.append(thread1)
thread2 = threading.Thread(target = doWaiting1)
thread2.start()
tsk.append(thread2)
print('start join: ' + time.strftime('%H:%M:%S') + "\n")
for tt in tsk:
tt.join(3)
print('end main thread: ' + time.strftime('%H:%M:%S') + "\n")
结果分析;