代码
import threading
import time
from threading import current_thread
def myThread(arg1, arg2):
print(current_thread().getName(), 'start')
print('%s %s' %(arg1, arg2))
time.sleep(1)
print(current_thread().getName(), 'stop')
for i in range(1, 6, 1): #1开始 ,5结束, 每次递增1
t1 = threading.Thread(target=myThread, args = (i, i + 1))
t1.start()
print(current_thread().getName(), 'end')
运行
主程序在线程结束之前结束
让线程先结束
代码
import threading
from threading import current_thread
class Mythread(threading.Thread):
def run(self):
print(current_thread().getName(), 'start')
print('run')
print(current_thread().getName(), 'stop')
t1 = Mythread()
t1.start() #线程的运行过程是调用的treading里的run方法,这里我们自定义run方法的内容
t1.join() #让线程先结束
print(current_thread().getName(), 'end')