from threading import Condition
class CountDownLatch: def __init__(self,count): self.count = count self.condition = Condition() def await(self): try: self.condition.acquire() while self.count > 0: self.condition.wait() finally: self.condition.release() def countDown(self): try: self.condition.acquire() self.count -= 1 self.condition.notifyAll() finally: self.condition.release() def getCount(self):return self.count
1. 定义一个Condition对象以及一个count信号量,在count信号量大于0的时候,condition一直处于wait;
2. 这样就可以协调一个任务与一组任务之间的关系,如果这一组任务没有执行完毕,则该任务一直等待;