Simple Solution using await & notify | java
Intuition\nHere i am using boolean flag and concept of bussy wait to check on condition and print something.\n\ni wait till the condition is not in desired stat
# Intuition\nHere i am using boolean flag and concept of bussy wait to check on condition and print something.\n\ni wait till the condition is not in desired state and once someOther thread notifies me the values has changed i check the condition again and proceed if the condition succeeds.\n\n# Code\n```\nclass FooBar {\n    private int n;\n    boolean printed;\n\n    public FooBar(int n) {\n        this.n = n;\n        printed = false;\n    }\n\n    public void foo(Runnable printFoo) throws InterruptedException {\n        \n        for (int i = 0; i < n; i++) {\n            \n        \t// printFoo.run() outputs "foo". Do not change or remove this line.\n        \tsynchronized (this) {\n                while (printed == true) {\n                    wait();\n                }\n\n                printFoo.run();\n                printed = true;\n                notify();\n            }    \n\n        }\n    }\n\n    public void bar(Runnable printBar) throws InterruptedException {\n        \n        for (int i = 0; i < n; i++) {\n            \n            // printBar.run() outputs "bar". Do not change or remove this line.\n        \tsynchronized (this) {\n                while (printed == false) { \n                    wait();\n                }\n                 printBar.run();\n                 printed = false;\n                 notify();\n            }\n        }\n    }\n}\n```\n\nBelow is another approach using reetrantLock\n\n# ReetrantLock Approach\nNote: similar to above approach\nwe need condition construct offered by lock to signal and wait onto\nwait ~ condition.await()\nnotify ~ condition.signal()\n# Code\n```\nclass FooBar {\n    private int n;\n    boolean printed;\n    Lock lock = new ReentrantLock();\n    Condition condition = lock.newCondition();\n\n    public FooBar(int n) {\n        this.n = n;\n        printed = false;\n    }\n\n    public void foo(Runnable printFoo) throws InterruptedException {\n        \n        for (int i = 0; i < n; i++) {\n            \n        \t// printFoo.run() outputs "foo". Do not change or remove this line.\n        \tlock.lock();\n                while (printed == true) {\n                    condition.await();\n                }\n\n                printFoo.run();\n                printed = true;\n                condition.signal();\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            \n            // printBar.run() outputs "bar". Do not change or remove this line.\n        \tlock.lock();\n                while (printed == false) { \n                    condition.await();\n                }\n                 printBar.run();\n                 printed = false;\n                 condition.signal();\n            lock.unlock();\n        }\n    }\n}\n```\n
['Java']