题目地址 https://leetcode-cn.com/problems/print-foobar-alternately/comments/
import queue
class FooBar:
def __init__(self, n):
self.n = n
self.q = queue.Queue(1)
def foo(self, printFoo: 'Callable[[], None]') -> None:
for i in range(self.n):
self.q.put(0)
# printFoo() outputs "foo". Do not change or remove this line.
printFoo()
def bar(self, printBar: 'Callable[[], None]') -> None:
for i in range(self.n):
self.q.get()
# printBar() outputs "bar". Do not change or remove this line.
printBar()

本文介绍了一个在LeetCode上的编程挑战,通过使用Python的queue模块实现同步控制,确保Foo和Bar两个字符串交替打印。此解决方案展示了如何在多线程环境下进行线程间的同步操作。
1242

被折叠的 条评论
为什么被折叠?



