JAVA solution using Semaphore
Complexity\n- Time complexity:\nO(1)\n\n- Space complexity:\nO(1)\n\n# Code\n\nclass Foo {\n\n    Semaphore first;\n    Semaphore second;\n    Semaphore third;\
# Complexity\n- Time complexity:\nO(1)\n\n- Space complexity:\nO(1)\n\n# Code\n```\nclass Foo {\n\n    Semaphore first;\n    Semaphore second;\n    Semaphore third;\n    public Foo() {\n        first = new Semaphore(1);\n        second = new Semaphore(0);\n        third = new Semaphore(0);\n    }\n\n    public void first(Runnable printFirst) throws InterruptedException {\n        first.acquire();\n        // printFirst.run() outputs "first". Do not change or remove this line.\n        printFirst.run();\n        second.release();\n    }\n\n    public void second(Runnable printSecond) throws InterruptedException {\n        second.acquire();\n        // printSecond.run() outputs "second". Do not change or remove this line.\n        printSecond.run();\n        third.release();\n    }\n\n    public void third(Runnable printThird) throws InterruptedException {\n        third.acquire();\n        // printThird.run() outputs "third". Do not change or remove this line.\n        printThird.run();\n        first.release();\n    }\n}\n```
['Java']