import threading
import time
class MyThread(threading.Thread):
def __init__(self, arg):
# 显式的调用父类的初始化函数。
#super(MyThread, self).__init__()
super().__init__()
self.arg = arg
# 定义每个线程要运行的函数
def start(self):
for i in range(5):
print("test{}:{}".format(self.arg, i + 1))
time.sleep(0.1)
if __name__ == "__main__":
t = MyThread('A')
t.daemon=True
t.start()
while True:
time.sleep(0.1)
[python]线程继承和参数传递
最新推荐文章于 2024-01-18 21:20:45 发布
本文介绍了如何在Python中创建和使用自定义线程类MyThread,展示了如何通过继承`threading.Thread`并重写`__init__`和`start`方法来实现线程功能。同时,还展示了如何设置守护线程和主程序的无限循环监控。
摘要由CSDN通过智能技术生成