Java ReentrantLock + Condition + Volatile
Please check my solution, simple to understand~  (faster than 99.43%, less than 96.24% memory)\nJava\nimport java.util.*;\n\nclass FooBar {\n    \n    private i
Please check my solution, simple to understand~  (faster than 99.43%, less than 96.24% memory)\n```Java\nimport java.util.*;\n\nclass FooBar {\n    \n    private int n;\n    private volatile boolean invokeFoo;\n    private final Lock lock = new ReentrantLock();\n    private Condition fooC = lock.newCondition();\n    private Condition barC = lock.newCondition();\n    \n\n    public FooBar(int n) {\n        this.n = n;\n        this.invokeFoo = true;\n    }\n\n    public void foo(Runnable printFoo) throws InterruptedException {\n        for (int i = 0; i < n; i++) {\n            try {\n                lock.lock();\n                if (!this.invokeFoo) {\n                    fooC.await();\n                }\n                printFoo.run();\n                this.invokeFoo = false;\n                barC.signal();\n            } finally {\n                lock.unlock();\n            }\n        }\n    }\n\n    public void bar(Runnable printBar) throws InterruptedException {\n        for (int i = 0; i < n; i++) {\n            try {\n                lock.lock();\n                if (this.invokeFoo) {\n                    barC.await();\n                }\n                printBar.run();\n                this.invokeFoo = true;\n                fooC.signal();\n            } finally {\n                lock.unlock();\n            }\n        }\n    }\n}\n```\n\nUsing ReentrantLock + condition instead of synchronized to give the go signal partitcular thread. This will be better than wait and notifyAll~
['Java']