Simple java solution
Using wait and notifyAll to print in order. \n``` \nvolatile int loc = 0;\n    public Foo() {\n      loc = 0;   \n    }\n\n    public void first(Runnable printF
Using wait and notifyAll to print in order. \n``` \nvolatile int loc = 0;\n    public Foo() {\n      loc = 0;   \n    }\n\n    public void first(Runnable printFirst) throws InterruptedException {\n        \n        // printFirst.run() outputs "first". Do not change or remove this line.\n        synchronized(this){\n            while(loc != 0){\n                this.wait();\n            }\n            loc = (loc + 1) % 3;\n            this.notifyAll();\n            printFirst.run();\n        }\n    }\n\n    public void second(Runnable printSecond) throws InterruptedException {\n        \n        // printSecond.run() outputs "second". Do not change or remove this line.\n         synchronized(this){\n            while(loc != 1){\n                this.wait();\n            }\n            loc = (loc + 1) % 3;\n            this.notifyAll();\n            printSecond.run();\n         }\n    }\n\n    public void third(Runnable printThird) throws InterruptedException {\n        \n        // printThird.run() outputs "third". Do not change or remove this line.\n         synchronized(this){\n            while(loc != 2){\n                this.wait();\n            }\n            loc = (loc + 1) % 3;\n            this.notifyAll();\n            printThird.run();\n         }\n    }
[]