使用Thread创建线程

#_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
主线程结束
posted @   Stary_tx  阅读(596)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示