import threading
class FooBar:
def __init__(self, n):
self.n = n
self.foo_lock = threading.Semaphore() #添加信号量计数器,初始值为1
self.foo_lock.acquire() # 令两个计数器的值为0,造成线程阻塞
self.bar_lock = threading.Semaphore()
self.bar_lock.acquire()
def foo(self, printFoo: 'Callable[[], None]') -> None:
for i in range(self.n):
# printFoo() outputs "foo". Do not change or remove this line.
printFoo()
self.bar_lock.release()
self.foo_lock.acquire()
def bar(self, printBar: 'Callable[[], None]') -> None:
for i in range(self.n):
self.bar_lock.acquire()
# printBar() outputs "bar". Do not change or remove this line.
printBar()
self.foo_lock.release()
foobar
最新推荐文章于 2020-05-04 15:35:55 发布