#_author:来童星
#date:2019/12/17
#使用threading模块创建线程
import threading,time
def process():
for i in range(3):
time.sleep(1)
print('thread name is %s'%threading.current_thread().name)
if __name__=='__main__':
print('主线程开始')
threads=[threading.Thread(target=process) for i in range(4)]# 创建4个线程存入列表
for t in threads:
t.start()
for t in threads:
t.join()
print('主线程结束')
运行结果:
主线程开始
thread name is Thread-4
thread name is Thread-2
thread name is Thread-1
thread name is Thread-3
thread name is Thread-1
thread name is Thread-4
thread name is Thread-3
thread name is Thread-2
thread name is Thread-3
thread name is Thread-1
thread name is Thread-4
thread name is Thread-2
主线程结束