Simple JAVA semaphore solution
\nclass FooBar {\n    private int n;\n    Semaphore fooSemaphore;\n    Semaphore barSemaphore;\n\n    public FooBar(int n) {\n        this.n = n;\n        fooSe
```\nclass FooBar {\n    private int n;\n    Semaphore fooSemaphore;\n    Semaphore barSemaphore;\n\n    public FooBar(int n) {\n        this.n = n;\n        fooSemaphore = new Semaphore(1);\n        barSemaphore = new Semaphore(0);\n    }\n\n    public void foo(Runnable printFoo) throws InterruptedException {\n        \n        for (int i = 0; i < n; i++) {\n            fooSemaphore.acquire();\n        \tprintFoo.run();\n            barSemaphore.release();\n        }\n    }\n\n    public void bar(Runnable printBar) throws InterruptedException {\n        for (int i = 0; i < n; i++) {\n            barSemaphore.acquire();\n        \tprintBar.run();\n            fooSemaphore.release();\n        }\n    }\n}\n```
[]