#_author:来童星
#date:2019/12/17
#使用Thread创建线程
import threading
import time
class Sunthread(threading.Thread):
def run(self):
for i in range(3):
time.sleep(1)
msg='子线程'+self.name+'执行,i='+str(i)
print(msg)
if __name__=='__main__':
print('主线程开始')
t1=Sunthread()
t2=Sunthread()
t1.start()
t2.start()
t1.join()
t2.join()
print('主线程结束')
运行结果:
主线程开始
子线程Thread-1执行,i=0
子线程Thread-2执行,i=0
子线程Thread-1执行,i=1
子线程Thread-2执行,i=1
子线程Thread-1执行,i=2
子线程Thread-2执行,i=2
主线程结束