代码
import time
def thread_status(t):
if t.is_alive():
print('still running')
else:
print('completed')
def countdown(n):
while n > 0:
print('T-minus', n)
n -= 1
time.sleep(2)
from threading import Thread
t = Thread(target=countdown, args=(10,))
t.start()
for i in range(10):
thread_status(t)
time.sleep(3)
class CountdownTask:
def __init__(self):
self._running = True
def terminate(self):
self._running = False
def run(self, n):
while self._running and n>0:
print('T-minus', n)
n -= 1
time.sleep(3)
#c = CountdownTask()
#t = Thread(target=c.run, args=(10,))
#t.start()
#time.sleep(10)
#c.terminate()