Synchronized With Wait & Notify [JAVA]
\nclass FooBar {\n    private int n;\n    boolean fooPrinted = false;\n    public FooBar(int n) {\n        this.n = n;\n    }\n\n    public synchronized void fo
```\nclass FooBar {\n    private int n;\n    boolean fooPrinted = false;\n    public FooBar(int n) {\n        this.n = n;\n    }\n\n    public synchronized void foo(Runnable printFoo) throws InterruptedException {\n        for (int i = 0; i < n; i++) {\n            if(fooPrinted) this.wait();\n            printFoo.run();\n            fooPrinted = true;\n            this.notify();\n\n        }   \n    }\n\n    public synchronized void bar(Runnable printBar) throws InterruptedException {\n        for (int i = 0; i < n; i++) {\n            if(!fooPrinted) this.wait();\n        \tprintBar.run();\n            fooPrinted = false;\n            this.notify();\n        }\n    }\n}\n```
['Java']