转发:https://blog.csdn.net/briblue/article/details/85101144
关于join方法补充:
join()
方法是Thread
类中的一个方法,该方法的定义是等待该线程终止。其实就是join()
方法将挂起调用线程(一般是main函数)的执行,直到被调用的对象完成它的执行。
补充一个例子:如果线程回调函数是含参的
import threading
import time
def test(nums,id):
for i in range(nums):
print(threading.current_thread().name+' test ',i,'id:',id)
time.sleep(0.5)
thread = threading.Thread(target=test,name='TestThread',args=(5,1))
thread.start()
thread.join()
for i in range(5):
print(threading.current_thread().name+' main ', i,'is alive:',thread.isAlive())
time.sleep(1)
TestThread test 0 id: 1
TestThread test 1 id: 1
TestThread test 2 id: 1
TestThread test 3 id: 1
TestThread test 4 id: 1
MainThread main 0 is alive: False
MainThread main 1 is alive: False
MainThread main 2 is alive: False
MainThread main 3 is alive: False
MainThread main 4 is alive: False