Java | Beat 80% simple semaphore solution
\nprivate int n;\n    private Semaphore sem1;\n    private Semaphore sem2;\n    public FooBar(int n) {\n        this.n = n;\n        //as foo print need happen 
```\nprivate int n;\n    private Semaphore sem1;\n    private Semaphore sem2;\n    public FooBar(int n) {\n        this.n = n;\n        //as foo print need happen first, thus Semaphore need to be 1.\n        sem1 = new Semaphore(1);\n        sem2 = new Semaphore(0);\n    }\n\n    public void foo(Runnable printFoo) throws InterruptedException {\n\n        for (int i = 0; i < n; i++) {\n            sem1.acquire();\n        \t// printFoo.run() outputs "foo". Do not change or remove this line.\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            sem2.acquire();\n            // printBar.run() outputs "bar". Do not change or remove this line.\n        \tprintBar.run();\n            sem1.release();\n        }\n    }\n```
['Java']