Using simple semaphores in Java
\nclass FooBar {\n    private int n;\n    private Semaphore foo_s = new Semaphore(1);\n    private Semaphore bar_s = new Semaphore(0); \n    \n\n    public FooB
```\nclass FooBar {\n    private int n;\n    private Semaphore foo_s = new Semaphore(1);\n    private Semaphore bar_s = new Semaphore(0); \n    \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              foo_s.acquire();\n        \t// printFoo.run() outputs "foo". Do not change or remove this line.\n        \tprintFoo.run();\n            bar_s.release();\n        }\n    }\n\n    public void bar(Runnable printBar) throws InterruptedException {\n       \n        for (int i = 0; i < n; i++) {\n           bar_s.acquire();     \n            // printBar.run() outputs "bar". Do not change or remove this line.\n        \tprintBar.run();\n            foo_s.release();\n        }\n    }\n}\n```
[]