[Java] 4 Java threading solutions (Synchronized,Lock,Volatile,CAS)
Overall,solution 2(lock+condition) performs better:\n comparing to solution 1,solution 2 using more waiting queues to avoid unnecessary notify, reduce lock comp
Overall,solution 2(lock+condition) performs better:\n* comparing to solution 1,solution 2 using more waiting queues to avoid unnecessary notify, reduce lock competition.\n* comparing to solution 3 and solution 4,solution 2 using synchronization queue to avoid unnecessary cpu execution.\n\n## 1.Synchronized(monitor exit happens-before monitor enter)\n\n```\n/**\n * @autor yeqiaozhu.\n * @date 2019-08-01\n */\npublic class FooBarSynchronized {\n\n\n    private int n;\n    //flag 0->foo to be print  1->foo has been printed\n    private int flag = 0;\n\n\n    public FooBarSynchronized(int n) {\n        this.n = n;\n    }\n\n    public void foo(Runnable printFoo) throws InterruptedException {\n\n        for (int i = 0; i < n; i++) {\n            synchronized (this) {\n                while (flag == 1) {\n                    this.wait();\n                }\n                // printFoo.run() outputs "foo". Do not change or remove this line.\n                printFoo.run();\n                flag = 1;\n                this.notifyAll();\n            }\n        }\n    }\n\n    public void bar(Runnable printBar) throws InterruptedException {\n\n        for (int i = 0; i < n; i++) {\n            synchronized (this) {\n                while (flag == 0) {\n                    this.wait();\n                }\n                // printBar.run() outputs "bar". Do not change or remove this line.\n                printBar.run();\n                flag = 0;\n                this.notifyAll();\n            }\n        }\n    }\n}\n```\n\n## 2.Lock(volatile write happens-before volatile read)\n```\n\nimport java.util.concurrent.locks.Condition;\nimport java.util.concurrent.locks.ReentrantLock;\n\nclass FooBar {\n\n    private int n;\n    //flag 0->foo to be print  1->foo has been printed\n    private int flag=0;\n    ReentrantLock reentrantLock= new ReentrantLock();\n    Condition fooPrintedCondition=reentrantLock.newCondition();\n    Condition barPrintedCondition=reentrantLock.newCondition();\n\n    public FooBar(int n) {\n        this.n = n;\n    }\n\n    public void foo(Runnable printFoo) throws InterruptedException {\n        \n        for (int i = 0; i < n; i++) {\n            try {\n                reentrantLock.lock();\n                while (flag ==1){\n                    barPrintedCondition.await();\n                }\n                // printFoo.run() outputs "foo". Do not change or remove this line.\n                printFoo.run();\n                flag=1;\n                fooPrintedCondition.signalAll();\n            } catch (InterruptedException e) {\n                e.printStackTrace();\n            } finally {\n                reentrantLock.unlock();\n            }\n        }\n    }\n\n    public void bar(Runnable printBar) throws InterruptedException {\n        \n        for (int i = 0; i < n; i++) {\n            reentrantLock.lock();\n            while (flag==0){\n                fooPrintedCondition.await();\n            }\n            // printBar.run() outputs "bar". Do not change or remove this line.\n        \tprintBar.run();\n            flag=0;\n        \tbarPrintedCondition.signalAll();\n        \treentrantLock.unlock();\n        }\n    }\n}\n```\n## 3.Voatile(volatile write happens-before volatile read)\n\n```\n\n/**\n * @autor yeqiaozhu.\n * @date 2019-08-01\n */\npublic class FooBarVolatile {\n\n    private int n;\n   //flag 0->foo to be print  1->foo has been printed  using volatile\n    private volatile int flag=0;\n\n\n    public FooBarVolatile(int n) {\n        this.n = n;\n    }\n\n    public void foo(Runnable printFoo) throws InterruptedException {\n\n        for (int i = 0; i < n; i++) {\n            while (true){\n                if(flag==0){\n                    printFoo.run();\n                    flag=1;\n                    break;\n                }\n                Thread.sleep(1);\n            }\n        }\n    }\n\n    public void bar(Runnable printBar) throws InterruptedException {\n\n        for (int i = 0; i < n; i++) {\n            while (true){\n                if(flag==1){\n                    printBar.run();\n                    flag=0;\n                    break;\n                }\n                Thread.sleep(1);\n            }\n        }\n    }\n}\n\n```\n\n## 4.CAS(volatile write happens-before volatile read)\n\n```\npackage P1115;\n\nimport java.util.concurrent.atomic.AtomicInteger;\n\n/**\n * @autor yeqiaozhu.\n * @date 2019-08-01\n */\npublic class FooBarCAS {\n\n\n        private int n;\n        ////flag 0->foo to be print  1->foo has been printed\n        private AtomicInteger flag=new AtomicInteger(0);\n\n\n\n        public FooBarCAS(int n) {\n            this.n = n;\n        }\n\n        public void foo(Runnable printFoo) throws InterruptedException {\n\n            for (int i = 0; i < n; i++) {\n                while (!flag.compareAndSet(0,1)){\n                    Thread.sleep(1);\n                }\n                printFoo.run();\n            }\n        }\n\n        public void bar(Runnable printBar) throws InterruptedException {\n\n            for (int i = 0; i < n; i++) {\n                while (!flag.compareAndSet(1,0)){\n                    Thread.sleep(1);\n                }\n                printBar.run();\n            }\n        }\n\n}\n\n```
[]