[Java] Simple solution using synchronised and wait
\nclass FooBar {\n    private int n;\n\n    public FooBar(int n) {\n        this.n = n;\n    }\n\n    public void foo(Runnable printFoo) throws InterruptedExcep
```\nclass FooBar {\n    private int n;\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            \n        \t// printFoo.run() outputs "foo". Do not change or remove this line.\n             synchronized(this) {\n                 \n                 printFoo.run();\n                 \n                 wait();\n                 \n             }\n        \t\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            synchronized(this) {\n                \n                notify();\n                printBar.run();\n                \n            }\n        \t\n        }\n    }\n}\n```
[]