[Java] Semaphore solution
\nimport java.util.concurrent.Semaphore;\nclass FooBar {\n    private int n;\n    Semaphore s = new Semaphore(0);\n    Semaphore s2 = new Semaphore(1);\n\n    p
```\nimport java.util.concurrent.Semaphore;\nclass FooBar {\n    private int n;\n    Semaphore s = new Semaphore(0);\n    Semaphore s2 = new Semaphore(1);\n\n    public FooBar(int n) {\n        this.n = n;\n    }\n\n    public void foo(Runnable printFoo) throws InterruptedException {\n\n        for (int i = 0; i < n; i++) {\n\n            // printFoo.run() outputs "foo". Do not change or remove this line.\n            s2.acquire();\n\n            printFoo.run();\n            s.release();\n\n        }\n    }\n\n    public void bar(Runnable printBar) throws InterruptedException {\n\n        for (int i = 0; i < n; i++) {\n\n            // printBar.run() outputs "bar". Do not change or remove this line.\n            s.acquire();\n\n            printBar.run();\n            s2.release();\n\n        }\n    }\n}\n```
[]