把源代码抄了一遍,姑且认为学习吧
import time from threading import Condition,Thread class MySemaphore: def __init__(self,value=1): self._value = value self._condition = Condition() def acquire(self): with self._condition: while self._value==0: self._condition.wait() else: self._value-=1 def release(self): with self._condition: self._value += 1 self._condition.notify() sem = MySemaphore(3) def mythred(): sem.acquire() time.sleep(2) print('gogogo !!!') sem.release() for i in range(1,22): t = Thread(target=mythred) t.start()