Easy to understand java solution
\nclass FooBar {\n    private int n;\n    int bar = 0;\n\n    public FooBar(int n) {\n        this.n = n;\n    }\n\n    public void foo(Runnable printFoo) throw
```\nclass FooBar {\n    private int n;\n    int bar = 0;\n\n    public FooBar(int n) {\n        this.n = n;\n    }\n\n    public void foo(Runnable printFoo) throws InterruptedException {\n        \n            \n            for (int i = 0; i < n; i++) {\n                synchronized(this) {\n                if(bar == 1) {\n                    wait();\n                }\n\n                // printFoo.run() outputs "foo". Do not change or remove this line.\n                printFoo.run();\n                bar=1;\n                notify();\n            }\n        }\n    }\n    \n\n    public void bar(Runnable printBar) throws InterruptedException {\n        \n        \n            for (int i = 0; i < n; i++) {\n                synchronized(this) {\n                if(bar==0) {\n                    wait();\n                }\n\n                // printBar.run() outputs "bar". Do not change or remove this line.\n                printBar.run();\n                bar = 0;\n                notify();\n            }\n        }\n    }\n}\n```
['Java']