import threading
import time
class test:
def __init__(self):
self.__lock = threading.Lock()
self.__thread = None
self.__stop = False
def start(self):
self.__stop = False
self.__thread = threading.Thread(target=self.__run, daemon=True)
self.__thread.start()
print("--------------start--------------")
def stop(self):
self.__stop = True
if self.__thread is not None:
self.__thread.join()
print("--------------stop--------------")
def __run(self):
num=0
while not self.__stop:
time.sleep(0.5)
num+=1
print(num)
a=test()
a.start()
time.sleep(5)
a.stop()
a.start()
time.sleep(5)
线程的开启与停止示例
最新推荐文章于 2024-08-19 13:32:52 发布