Leetcode 1115. Print FooBar Alternately
📅2023-07-22🧐95
This problem is quite fun, and this is more related to the real world problems. Sometimes we deal with UI related problem, or multi-thread programs, we could use threading
from threading import Lock
class FooBar:
def __init__(self, n):
self.n = n
self.lock1 = Lock()
self.lock2 = Lock()
self.lock1.acquire()
def foo(self, printFoo: 'Callable[[], None]') -> None:
for i in range(self.n):
self.lock2.acquire()
printFoo()
self.lock1.release()
def bar(self, printBar: 'Callable[[], None]') -> None:
for i in range(self.n):
self.lock1.acquire()
printBar()
self.lock2.release()