多线程2thread
增加.join(),是主线程在最后结束:
1 import threading 2 from threading import current_thread 3 4 def myThread(arg1, arg2): 5 print(threading.current_thread().getName(), 'start') # 获取线程名字 6 print(arg1, '\t', arg2) 7 print(threading.current_thread().getName(), 'stop') 8 9 10 class mythread1(threading.Thread): # 定义threading.Thread的一个子类 11 def run(self): 12 print(current_thread().getName(),'start') 13 print('run') 14 print(current_thread().getName(), 'stop') 15 16 17 for i in range(1, 4, 1): 18 t1 = threading.Thread(target=myThread,args=(i, i * 2)) # 定义线程 19 t1.start() # 运行线程 20 t1.join() # 让主线程最后结束 21 print('----分割线----') 22 t2 = mythread1() 23 t2.start() 24 t2.join() 25 print(threading.current_thread().getName(), 'End')