import threading import time class CustomThread(threading.Thread): def __init__(self, thread_name): # step 1: call base __init__ function super(CustomThread, self).__init__(name=thread_name) self._tname = thread_name def run(self): # step 2: overide run function time.sleep(0.5) print("This is %s running...." % self._tname) if __name__ == "__main__": t1 = CustomThread("thread 1") t2 = CustomThread("thread 2") t1.setDaemon(False) t2.setDaemon(False) t1.start() t2.start() print("This is main function")
继承threading.Thread,并重写run
最新推荐文章于 2024-07-12 16:26:30 发布