'''
判断线程是否已经启动
'''
from threading import Thread, Event
import threading
import time


def countdown(n, started_evt):
    print('countdown starting')
    started_evt.set()
    while n > 0:
        print('T-minus', n)
        n -= 1
        time.sleep(5)


class PeriodicTimer:
    def __init__(self, interval):
        self._interval = interval
        self._flag = 0
        self._cv = threading.Condition()

    def start(self):
        t = threading.Thread(target=self.run)
        t.daemon = Thread
        t.start()

    def run(self):
        while True:
            time.sleep(self._interval)
            with self._cv:
                self._flag ^= 1
                self._cv.notify_all()

    def wait_for_tick(self):
        with self._cv:
            last_flag = self._flag
            while last_flag == self._flag:
                self._cv.wait()


ptimer = PeriodicTimer(5)
ptimer.start()


def countdown2(nticks):
    while nticks > 0:
        ptimer.wait_for_tick()
        print('T-minus', nticks)
        nticks -= 1


def countup(last):
    n = 0
    while n < last:
        ptimer.wait_for_tick()
        print('Counting', n)
        n += 1


if __name__ == "__main__":
    threading.Thread(target=countdown2, args=(10,)).start()
    threading.Thread(target=countup, args=(5,)).start()

    # start_evt = Event()
    # print('Launching countdown')
    # t = Thread(target=countdown, args=(10, start_evt))
    # t.start()
    #
    # start_evt.wait()
    # print('countdown is running')
