Java very easy Semaphore Solution
class FooBar {\n    private int n;\n\n    public FooBar(int n) {\n        this.n = n;\n    }\n    \n    Semaphore sem1 = new Semaphore(0);\n    Semaphore sem2 =
class FooBar {\n    private int n;\n\n    public FooBar(int n) {\n        this.n = n;\n    }\n    \n    Semaphore sem1 = new Semaphore(0);\n    Semaphore sem2 = new Semaphore(0);\n\n    public void foo(Runnable printFoo) throws InterruptedException {\n        \n        for (int i = 0; i < n; i++) {\n            \n        \tsem1.acquire();\n        \tprintFoo.run();\n            sem2.release();\n        }\n    }\n\n    public void bar(Runnable printBar) throws InterruptedException {\n        \n        for (int i = 0; i < n; i++) {\n            \n            sem1.release();\n        \tprintBar.run();\n            sem2.acquire();\n        }\n    }\n}
[]