[Java] Simplest solution using volatile variable
Here, volatile keyword makes the variable threadsafe and making it visible to all threads works.\n\nJava\nclass Foo {\n\n    volatile int methodCompleted;\n    
Here, `volatile` keyword makes the variable threadsafe and making it visible to all threads works.\n\n```Java\nclass Foo {\n\n    volatile int methodCompleted;\n    \n    public Foo() {\n        methodCompleted = 0;\n    }\n\n    public void first(Runnable printFirst) throws InterruptedException {\n        printFirst.run();\n        methodCompleted =  1;\n    }\n\n    public void second(Runnable printSecond) throws InterruptedException {\n        while (methodCompleted != 1) ;\n        printSecond.run();\n        methodCompleted =  2;\n    }\n\n    public void third(Runnable printThird) throws InterruptedException {\n        while (methodCompleted != 2) ;\n        printThird.run();\n        methodCompleted =  3;\n    }\n}\n\n```
['Java']