Java | ReentrantLock with Condition
\nclass FooBar {\n    private int n;\n    \n    Lock lock = new ReentrantLock();\n    Condition fooCondition = lock.newCondition();\n    volatile boolean isFoo 
```\nclass FooBar {\n    private int n;\n    \n    Lock lock = new ReentrantLock();\n    Condition fooCondition = lock.newCondition();\n    volatile boolean isFoo = true;\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            lock.lock();\n            try{\n                while(!isFoo) fooCondition.await();\n                // printFoo.run() outputs "foo". Do not change or remove this line.\n        \t    printFoo.run();\n                isFoo = false;\n                fooCondition.signal();\n            }finally{\n                lock.unlock();\n            }              \n        }\n    }\n\n    public void bar(Runnable printBar) throws InterruptedException {\n        \n        for (int i = 0; i < n; i++) {\n            lock.lock();\n            try{\n                while(isFoo) fooCondition.await();\n                // printBar.run() outputs "bar". Do not change or remove this line.\n        \t    printBar.run();\n                isFoo = true;\n                fooCondition.signal();\n            }finally{\n                lock.unlock();\n            }   \n        }\n    }\n}\n```
[]