Java old school wait/notify solution
counter variable can be replaced with boolean type but if you want to track the number of invocation it is worth keeping int\n\nclass FooBar {\n    private int 
`counter` variable can be replaced with `boolean` type but if you want to track the number of invocation it is worth keeping `int`\n```\nclass FooBar {\n    private int n;\n    private volatile int counter = 1;\n\n    public FooBar(int n) {\n        this.n = n;\n    }\n\n\n    public void foo(Runnable printFoo) throws InterruptedException {\n\n        for (int i = 0; i < n; i++) {\n\n            synchronized (this) {\n                while (counter % 2 == 0) {\n                    wait();\n                }\n                // printFoo.run() outputs "foo". Do not change or remove this line.\n                printFoo.run();\n                counter++;\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            synchronized (this) {\n                while (counter % 2 != 0) {\n                    wait();\n                }\n                // printBar.run() outputs "bar". Do not change or remove this line.\n                printBar.run();\n                counter++;\n                notify();\n            }\n        }\n    }\n}\n```
[]