Java Volatile flag and Thread.sleep(1) to skip TLE
\nclass FooBar {\n    private int n;\n    private volatile boolean flag = true;\n\n    \n    public FooBar(int n) {\n        this.n = n;\n    }\n\n    public vo
```\nclass FooBar {\n    private int n;\n    private volatile boolean flag = true;\n\n    \n    public FooBar(int n) {\n        this.n = n;\n    }\n\n    public void foo(Runnable printFoo) throws InterruptedException {\n        \n        for (int i = 0; i < n; i++) {\n            while(!flag){\n                // w/o this line "Submit" return Time Limit Exceeded, idk why\n                Thread.sleep(1);\n            }\n        \tprintFoo.run();\n            this.flag=false;\n            \n            \n        }\n    }\n\n    public void bar(Runnable printBar) throws InterruptedException {\n        \n        for (int i = 0; i < n; i++) {\n            while(flag){\n                // w/o this line "Submit" return Time Limit Exceeded,  idk why\n                Thread.sleep(1);\n            }\n        \tprintBar.run();\n            this.flag=true;\n            \n            \n        }\n    }\n}\n```
[]