Python使用类创建多线程(二)

Python使用类创建多线程(二)

使用类创建多线程

import time
import threading
#创建mythread类,继承threading.Thread父类
class mythread(threading.Thread):
	def __init__(self,name = 'python'):
		super().__init__()
		self.name = name
	
	def run(self):
		for i in range(2):
			print('hello',self.name)
			time.sleep(1)	
if __name__ == "__main__":
	#无参数
	t1 = mythread()
	#有参数
	t2 = mythread("yangyukuan")
	t1.start()
	t2.start()

运行结果;

hello python
hello yangyukuan
hello python
hello yangyukuan

结果同利用函数创建 多线程是一样的。

猜你喜欢

转载自blog.csdn.net/weixin_39549161/article/details/85655022
今日推荐