import threading
class CustomThread(threading.Thread):
# 提供构造方法
def __init__(self, info1, info2):
self.info1 = info1
self.info2 = info2
# 如果子类提供了构造方法,默认不会调用父类的构造方法,需要自己手动调用
# 建议大家如果以后自己提供了构造方法需要使用super调用父类的构造方法
super(CustomThread, self).__init__()
# 自定义线程类里面封装一系列线程相关的任务
def show_info1(self):
print(self.info1)
def show_info2(self):
print(self.info2)
# 执行对应的任务, 其实是重写父类的run方法
def run(self):
self.show_info1()
self.show_info2()
custom_thread.start()
# 自定义线程类
# 提供构造方法
def __init__(self, info1, info2):
self.info1 = info1
self.info2 = info2
# 如果子类提供了构造方法,默认不会调用父类的构造方法,需要自己手动调用
# 建议大家如果以后自己提供了构造方法需要使用super调用父类的构造方法
super(CustomThread, self).__init__()
# 自定义线程类里面封装一系列线程相关的任务
def show_info1(self):
print(self.info1)
def show_info2(self):
print(self.info2)
# 执行对应的任务, 其实是重写父类的run方法
def run(self):
self.show_info1()
self.show_info2()
# 创建自定义线程对象,
#提示: 不要这里使用target执行某个任务,因为执行任务同一在run方法里面执行的
custom_thread = CustomThread("show_info1", "show_info2")
# 提示:不要直接调用run方法,因为start方法内部已经调用run方法,启动线程统一start方法custom_thread.start()