JAVA Boolean Volatile solution
\nclass FooBar {\n        private int n;\n\n        volatile boolean callFoo;\n        volatile boolean callBar;\n\n        public FooBar(int n) {\n            
```\nclass FooBar {\n        private int n;\n\n        volatile boolean callFoo;\n        volatile boolean callBar;\n\n        public FooBar(int n) {\n            this.n = n;\n            callFoo = true;\n            callBar = false;\n        }\n\n        public synchronized void foo(Runnable printFoo) throws InterruptedException {\n\n            for (int i = 0; i < n; i++) {\n                while (callBar) {\n                    wait();\n                }\n                // printFoo.run() outputs "foo". Do not change or remove this line.\n                printFoo.run();\n                callFoo = false;\n                callBar = true;\n                notifyAll();\n            }\n        }\n\n        public synchronized void bar(Runnable printBar) throws InterruptedException {\n\n            for (int i = 0; i < n; i++) {\n                while (callFoo) {\n                    wait();\n                }\n                // printBar.run() outputs "bar". Do not change or remove this line.\n                printBar.run();\n                callBar = false;\n                callFoo = true;\n                notifyAll();\n            }\n        }\n    }\n```
[]