Java Solution with simple atomic boolean
Simple AtomicBoolean. However, while loops will eat out CPU cycles.\n\nclass FooBar {\n    private int n;\n    private AtomicBoolean acquire;\n    \n    public 
Simple AtomicBoolean. However, while loops will eat out CPU cycles.\n```\nclass FooBar {\n    private int n;\n    private AtomicBoolean acquire;\n    \n    public FooBar(int n)  {\n        this.n = n;\n        acquire = new AtomicBoolean(true);\n\n    }\n\n    public void foo(Runnable printFoo) throws InterruptedException {\n        for (int i = 0; i < n; i++) {\n            while (!acquire.get()){ }\n        \t// printFoo.run() outputs "foo". Do not change or remove this line.\n        \tprintFoo.run();\n            acquire.compareAndSet(true, false);\n        }\n\n    }\n\n    public void bar(Runnable printBar) throws InterruptedException {\n        for (int i = 0; i < n; i++) {\n           while (acquire.get()){}\n            // printBar.run() outputs "bar". Do not change or remove this line.\n        \tprintBar.run();\n            acquire.compareAndSet(false, true);\n        }\n    }\n}\n```
[]