返回介绍

solution / 1100-1199 / 1115.Print FooBar Alternately / README_EN

发布于 2024-06-17 01:03:23 字数 4986 浏览 0 评论 0 收藏 0

1115. Print FooBar Alternately

中文文档

Description

Suppose you are given the following code:

class FooBar {
  public void foo() {
  for (int i = 0; i < n; i++) {
    print("foo");
  }
  }

  public void bar() {
  for (int i = 0; i < n; i++) {
    print("bar");
  }
  }
}

The same instance of FooBar will be passed to two different threads:

  • thread A will call foo(), while
  • thread B will call bar().

Modify the given program to output "foobar" n times.

 

Example 1:

Input: n = 1
Output: "foobar"
Explanation: There are two threads being fired asynchronously. One of them calls foo(), while the other calls bar().
"foobar" is being output 1 time.

Example 2:

Input: n = 2
Output: "foobarfoobar"
Explanation: "foobar" is being output 2 times.

 

Constraints:

  • 1 <= n <= 1000

Solutions

Solution 1: Multithreading + Semaphore

We use two semaphores $f$ and $b$ to control the execution order of the two threads, where $f$ is initially set to $1$ and $b$ is set to $0$, indicating that thread $A$ executes first.

When thread $A$ executes, it first performs the $acquire$ operation on $f$, which changes the value of $f$ to $0$. Thread $A$ then gains the right to use $f$ and can execute the $foo$ function. After that, it performs the $release$ operation on $b$, changing the value of $b$ to $1$. This allows thread $B$ to gain the right to use $b$ and execute the $bar$ function.

When thread $B$ executes, it first performs the $acquire$ operation on $b$, which changes the value of $b$ to $0$. Thread $B$ then gains the right to use $b$ and can execute the $bar$ function. After that, it performs the $release$ operation on $f$, changing the value of $f$ to $1$. This allows thread $A$ to gain the right to use $f$ and execute the $foo$ function.

Therefore, we only need to loop $n$ times, each time executing the $foo$ and $bar$ functions, first performing the $acquire$ operation, and then the $release$ operation.

The time complexity is $O(n)$, and the space complexity is $O(1)$.

from threading import Semaphore


class FooBar:
  def __init__(self, n):
    self.n = n
    self.f = Semaphore(1)
    self.b = Semaphore(0)

  def foo(self, printFoo: "Callable[[], None]") -> None:
    for _ in range(self.n):
      self.f.acquire()
      # printFoo() outputs "foo". Do not change or remove this line.
      printFoo()
      self.b.release()

  def bar(self, printBar: "Callable[[], None]") -> None:
    for _ in range(self.n):
      self.b.acquire()
      # printBar() outputs "bar". Do not change or remove this line.
      printBar()
      self.f.release()
class FooBar {
  private int n;
  private Semaphore f = new Semaphore(1);
  private Semaphore b = new Semaphore(0);

  public FooBar(int n) {
    this.n = n;
  }

  public void foo(Runnable printFoo) throws InterruptedException {
    for (int i = 0; i < n; i++) {
      f.acquire(1);
      // printFoo.run() outputs "foo". Do not change or remove this line.
      printFoo.run();
      b.release(1);
    }
  }

  public void bar(Runnable printBar) throws InterruptedException {
    for (int i = 0; i < n; i++) {
      b.acquire(1);
      // printBar.run() outputs "bar". Do not change or remove this line.
      printBar.run();
      f.release(1);
    }
  }
}
#include <semaphore.h>

class FooBar {
private:
  int n;
  sem_t f, b;

public:
  FooBar(int n) {
    this->n = n;
    sem_init(&f, 0, 1);
    sem_init(&b, 0, 0);
  }

  void foo(function<void()> printFoo) {
    for (int i = 0; i < n; i++) {
      sem_wait(&f);
      // printFoo() outputs "foo". Do not change or remove this line.
      printFoo();
      sem_post(&b);
    }
  }

  void bar(function<void()> printBar) {
    for (int i = 0; i < n; i++) {
      sem_wait(&b);
      // printBar() outputs "bar". Do not change or remove this line.
      printBar();
      sem_post(&f);
    }
  }
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文