[JAVA] Very Simple Synchronized Solution
\nclass FooBar {\n    private int n;\n    boolean runFoo; // boolean to check if Thread A (the one calling foo()) is in execution\n    \n    public FooBar(int n
``` \nclass FooBar {\n    private int n;\n    boolean runFoo; // boolean to check if Thread A (the one calling foo()) is in execution\n    \n    public FooBar(int n) {\n        this.n = n;\n        runFoo = true;\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                // wait until bar() is running\n\t\t\t\twhile(!runFoo) {\n                    wait();\n                }\n                printFoo.run();\n                runFoo = false;\n                notifyAll();\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            synchronized(this) {\n\t\t\t\t// wait until foo() is running\n                while(runFoo) {\n                    wait();\n                }\n                printBar.run();\n                runFoo = true;\n                notifyAll();\n            }\n        }\n    }\n}\n```
['Java']