All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g1101_1200.s1115_print_foobar_alternately.FooBar Maven / Gradle / Ivy

There is a newer version: 1.38
Show newest version
package g1101_1200.s1115_print_foobar_alternately;

// #Medium #Concurrency #2023_06_01_Time_24_ms_(38.82%)_Space_43.5_MB_(7.54%)

import java.util.concurrent.Semaphore;

/**
 * 1115 - Print FooBar Alternately\.
 *
 * Medium
 *
 * 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`
**/
@SuppressWarnings("java:S2142")
public class FooBar {
    private int n;

    private final Semaphore fooSemaphore;
    private final Semaphore barSemaphore;

    public FooBar(int n) {
        this.n = n;
        fooSemaphore = new Semaphore(1);
        barSemaphore = new Semaphore(1);
        try {
            barSemaphore.acquire();
        } catch (InterruptedException ignored) {
            // ignored
        }
    }

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

    public void bar(Runnable printBar) throws InterruptedException {
        for (int i = 0; i < n; i++) {
            barSemaphore.acquire();
            // printBar.run() outputs "bar". Do not change or remove this line.
            printBar.run();
            fooSemaphore.release();
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy