Java synchronized version
\nclass FooBar {\n    private int n;\n    private Object lock;\n    private int count;\n\n    public FooBar(int n) {\n        this.n = n;\n        this.lock = n
```\nclass FooBar {\n    private int n;\n    private Object lock;\n    private int count;\n\n    public FooBar(int n) {\n        this.n = n;\n        this.lock = new Object();\n        this.count = 1;\n    }\n\n    public void foo(Runnable printFoo) throws InterruptedException {\n        for (int i = 0; i < n; i++) {\n            synchronized (lock) {\n                while(count != 1) {\n                    lock.wait();\n                }\n                printFoo.run();\n                count = 0;\n                lock.notifyAll();\n            }\n        }\n    }\n\n    public void bar(Runnable printBar) throws InterruptedException {\n\n        for (int i = 0; i < n; i++) {\n            synchronized(lock) {\n                while(count != 0) {\n                    lock.wait();\n                }\n                printBar.run();\n                count = 1;\n                lock.notifyAll();\n            }\n\n        }\n    }\n}\n\n```
[]