Semaphore Solution | Simple Java code
\nclass Foo {\n\n    private Semaphore forTwo;\n    private Semaphore forThree;\n    \n    public Foo() {\n        forTwo = new Semaphore(0);\n        forThree 
```\nclass Foo {\n\n    private Semaphore forTwo;\n    private Semaphore forThree;\n    \n    public Foo() {\n        forTwo = new Semaphore(0);\n        forThree = new Semaphore(0);\n    }\n\n\n    \n    public void first(Runnable printFirst) throws InterruptedException {\n        \n        // printFirst.run() outputs "first". Do not change or remove this line.\n        printFirst.run();\n        forTwo.release();\n    }\n\n    public void second(Runnable printSecond) throws InterruptedException {\n        \n        forTwo.acquire();\n        \n        // printSecond.run() outputs "second". Do not change or remove this line.\n        printSecond.run();\n        \n        forThree.release();\n    }\n\n    public void third(Runnable printThird) throws InterruptedException {\n        \n        \n        forThree.acquire();\n        \n        // printThird.run() outputs "third". Do not change or remove this line.\n        printThird.run();\n    }\n}\n```
[]