Java Simple Solution using volatile (97% faster)
\nclass Foo {\n\n    public volatile int order;\n    public Foo() {\n        order= 0;\n    }\n\n    public void first(Runnable printFirst) throws InterruptedEx
```\nclass Foo {\n\n    public volatile int order;\n    public Foo() {\n        order= 0;\n    }\n\n    public void first(Runnable printFirst) throws InterruptedException {\n        // printFirst.run() outputs "first". Do not change or remove this line.\n        printFirst.run();\n        order++;\n    }\n\n    public void second(Runnable printSecond) throws InterruptedException {\n        while(order!=1);\n        // printSecond.run() outputs "second". Do not change or remove this line.\n        printSecond.run();\n        order++;\n    }\n\n    public void third(Runnable printThird) throws InterruptedException {\n        while(order!=2);\n        // printThird.run() outputs "third". Do not change or remove this line.\n        printThird.run();\n    }\n}\n```
[]