js8510
2020-06-17 09:08:59 +08:00
哇。leetcode 都有一千多题了呀。我还真在 在生产环境见过类似的情况。但是实现和你发的这个有不同
作者的意图应该是这样,
···
from threading import Lock
class FooBar:
def __init__(self, n):
self.n = n
self.foo_lock = Lock()
self.bar_lock = Lock()
self.bar_lock.acquire() # <---- 1
def foo(self, printFoo: 'Callable[[], None]') -> None:
for i in range(self.n):
self.foo_lock.acquire() # <---- 2 | # <---- 5, blocked here and wait for foo_lock
# printFoo() outputs "foo". Do not change or remove this line.
printFoo() # <---- 3
self.bar_lock.release() # <---- 4
def bar(self, printBar: 'Callable[[], None]') -> None:
for i in range(self.n):
self.bar_lock.acquire() # <---- 2 and blocked here and wait for bar_lock
# printBar() outputs "bar". Do not change or remove this line.
printBar() # <---- 5
self.foo_lock.release() # <---- 6
···