代码如下:
import threading, time
class Test1(threading.Thread):
def __init__(self, name):
super().__init__(name=name)
def run(self):
print('test1 start')
time.sleep(2)
print('test1 end')
class Test2(threading.Thread):
def __init__(self, name):
super().__init__(name=name)
# 继承thread必须覆盖的方法
def run(self):
print('test2 start')
time.sleep(4)
print('test2 end')
if __name__ == '__main__':
thread1 = Test1('test1')
thread2 = Test2('test2')
start_time = time.time()
thread1.start()
thread2.start()
# 使用join方法,这两个线程就会阻塞主线程的运行,print('main')就会后执行
thread1.join()
thread2.join()
print('main')
结果: