Simple 6 lines java Explained Solution
Intutuion\n1. Use volatile variable which will update variable \'a\' in different caches of multi core system. Note - It is only required if this code runs on m
Intutuion\n1. Use volatile variable which will update variable \'a\' in different caches of multi core system. Note - It is only required if this code runs on multi core environment.\n2. Run printFirst.run only when a=1;\n3. Run printSecond.run only when a=2; \n4. Similarly printThird.run only when a=3l\n```\nclass Foo {\n    volatile int a;\n    public Foo() {\n        a=1;\n    }\n\n    public void first(Runnable printFirst) throws InterruptedException {\n        \n        // printFirst.run() outputs "first". Do not change or remove this line.\n        while(a!=1);\n        printFirst.run();\n        a=2;\n    }\n\n    public void second(Runnable printSecond) throws InterruptedException {\n        \n        // printSecond.run() outputs "second". Do not change or remove this line.\n        while(a!=2);\n        printSecond.run();\n        a=3;\n    }\n\n    public void third(Runnable printThird) throws InterruptedException {\n        \n        // printThird.run() outputs "third". Do not change or remove this line.\n        while(a!=3);\n        printThird.run();\n    }\n}\n```
[]