Java solution beats 100% in both time and space
\nclass Foo {\n    \n    private volatile boolean onePrinted;\n    private volatile boolean twoPrinted;\n\n    public Foo() {\n        onePrinted = false;\n    
```\nclass Foo {\n    \n    private volatile boolean onePrinted;\n    private volatile boolean twoPrinted;\n\n    public Foo() {\n        onePrinted = false;\n        twoPrinted = false;        \n    }\n\n    public synchronized void first(Runnable printFirst) throws InterruptedException {\n        \n        // printFirst.run() outputs "first". Do not change or remove this line.\n        printFirst.run();\n        onePrinted = true;\n        notifyAll();\n    }\n\n    public synchronized void second(Runnable printSecond) throws InterruptedException {\n        while(!onePrinted) {\n            wait();\n        }\n        // printSecond.run() outputs "second". Do not change or remove this line.\n        printSecond.run();\n        twoPrinted = true;\n        notifyAll();\n    }\n\n    public synchronized void third(Runnable printThird) throws InterruptedException {\n        while(!twoPrinted) {\n            wait();\n        }\n        // printThird.run() outputs "third". Do not change or remove this line.\n        printThird.run();\n    }\n}\n```
[]