{"id":1248,"date":"2024-07-29T00:12:39","date_gmt":"2024-07-28T16:12:39","guid":{"rendered":"http:\/\/www.spiceandwolf.com.cn\/?p=1248"},"modified":"2024-08-25T00:45:32","modified_gmt":"2024-08-24T16:45:32","slug":"juc-instroment","status":"publish","type":"post","link":"http:\/\/www.spiceandwolf.com.cn\/index.php\/juc-instroment\/","title":{"rendered":"JUC\u5e76\u53d1\u7f16\u7a0b\u4ecb\u7ecd"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">JUC\u5e76\u53d1\u5305\u4ecb\u7ecd<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>java.util.concurrent\u3002\u5e76\u53d1\u7f16\u7a0b\u5b9e\u7528\u5de5\u5177\u3002<\/li>\n\n\n\n<li>java.util.concurrent.atomic\u3002\u4e00\u4e2a\u5c0f\u7684\u7c7b\u5de5\u5177\u5305\uff0c\u652f\u6301\u5bf9\u5355\u4e2a\u53d8\u91cf\u8fdb\u884c\u65e0\u9501\u7ebf\u7a0b\u5b89\u5168\u7f16\u7a0b\u3002<\/li>\n\n\n\n<li>java.util.concurrent.locks\u3002\u63a5\u53e3\u548c\u7c7b\u63d0\u4f9b\u4e86\u7528\u4e8e\u9501\u5b9a\u548c\u7b49\u5f85\u6761\u4ef6\u7684\u6846\u67b6\u3002\u8fd9\u4e9b\u6761\u4ef6\u4e0d\u540c\u4e8e\u5185\u7f6e\u7684\u540c\u6b65\u548c\u76d1\u89c6\u5668\u3002<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">JMM(Java Memory Model\uff0cjava\u5185\u5b58\u6a21\u578b)<\/h2>\n\n\n\n<p>JMM\u662f\u6307java\u5185\u5b58\u6a21\u578b\uff0c\u4e0d\u662fJVM\uff0c\u4e0d\u662f\u6240\u8c13\u7684\u6808\u3001\u5806\u3001\u65b9\u6cd5\u533a\u3002<br>\u6bcf\u4e2aJava\u7ebf\u7a0b\u90fd\u6709\u81ea\u5df1\u7684\u5de5\u4f5c\u5185\u5b58\u3002\u64cd\u4f5c\u6570\u636e\uff0c\u9996\u5148\u4ece\u4e3b\u5185\u5b58\u4e2d\u8bfb\uff0c\u5f97\u5230\u4e00\u4efd\u62f7\u8d1d\uff0c\u64cd\u4f5c\u5b8c\u6bd5\u540e\u518d\u5199\u56de\u5230\u4e3b\u5185\u5b58\u3002<br>\u7531\u4e8eJVM\u8fd0\u884c\u7a0b\u5e8f\u7684\u5b9e\u4f53\u662f\u7ebf\u7a0b\uff0c\u800c\u6bcf\u4e2a\u7ebf\u7a0b\u521b\u5efa\u65f6JVM\u90fd\u4f1a\u4e3a\u5176\u521b\u5efa\u4e00\u4e2a\u5de5\u4f5c\u5185\u5b58\uff08\u6709\u4e9b\u5730\u65b9\u79f0\u4e3a\u6808\u7a7a\u95f4\uff09\uff0c\u5de5\u4f5c\u5185\u5b58\u662f\u6bcf\u4e2a\u7ebf\u7a0b\u7684\u79c1\u6709\u6570\u636e\u533a\u57df\uff0c\u800cJava\u5185\u5b58\u6a21\u578b\u4e2d\u89c4\u5b9a\u6240\u6709\u53d8\u91cf\u90fd\u5b58\u50a8\u5728\u4e3b\u5185\u5b58\uff0c\u4e3b\u5185\u5b58\u662f\u5171\u4eab\u5185\u5b58\uff0c\u6240\u6709\u7ebf\u7a0b\u90fd\u53ef\u4ee5\u8bbf\u95ee\uff0c\u4f46\u662f\u7ebf\u7a0b\u5bf9\u53d8\u91cf\u7684\u64cd\u4f5c\uff08\u8bfb\u53d6\u8d4b\u503c\u7b49\uff09\u5fc5\u987b\u5728\u5de5\u4f5c\u5185\u5b58\u4e2d\u8fdb\u884c\uff0c\u9996\u5148\u8981\u5c06\u53d8\u91cf\u4ece\u4e3b\u5185\u5b58\u62f7\u8d1d\u5230\u81ea\u5df1\u7684\u5de5\u4f5c\u5185\u5b58\u7a7a\u95f4\uff0c\u7136\u540e\u5bf9\u53d8\u91cf\u8fdb\u884c\u64cd\u4f5c\uff0c\u64cd\u4f5c\u5b8c\u6210\u540e\u518d\u5c06\u53d8\u91cf\u5199\u56de\u4e3b\u5185\u5b58\uff0c\u4e0d\u80fd\u76f4\u63a5\u64cd\u4f5c\u4e3b\u5185\u5b58\u4e2d\u7684\u53d8\u91cf\uff0c\u5404\u4e2a\u7ebf\u7a0b\u4e2d\u7684\u5de5\u4f5c\u5185\u5b58\u4e2d\u5b58\u50a8\u7740\u4e3b\u5185\u5b58\u4e2d\u7684\u53d8\u91cf\u526f\u672c\u62f7\u8d1d\uff0c\u56e0\u6b64\u4e0d\u540c\u7684\u7ebf\u7a0b\u95f4\u65e0\u6cd5\u8bbf\u95ee\u5bf9\u65b9\u7684\u5de5\u4f5c\u5185\u5b58\uff0c\u7ebf\u7a0b\u95f4\u7684\u901a\u4fe1\uff08\u4f20\u503c\uff09\u5fc5\u987b\u901a\u8fc7\u4e3b\u5185\u5b58\u6765\u5b8c\u6210\uff0c\u671f\u95f4\u8bbf\u95ee\u8fc7\u7a0b\u5982\u4e0b\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"737\" src=\"http:\/\/www.spiceandwolf.com.cn\/wp-content\/uploads\/2024\/07\/image-57-1024x737.png\" alt=\"\" class=\"wp-image-1251\" style=\"width:736px;height:auto\" srcset=\"http:\/\/www.spiceandwolf.com.cn\/wp-content\/uploads\/2024\/07\/image-57-1024x737.png 1024w, http:\/\/www.spiceandwolf.com.cn\/wp-content\/uploads\/2024\/07\/image-57-300x216.png 300w, http:\/\/www.spiceandwolf.com.cn\/wp-content\/uploads\/2024\/07\/image-57-768x552.png 768w, http:\/\/www.spiceandwolf.com.cn\/wp-content\/uploads\/2024\/07\/image-57-750x540.png 750w, http:\/\/www.spiceandwolf.com.cn\/wp-content\/uploads\/2024\/07\/image-57-110x80.png 110w, http:\/\/www.spiceandwolf.com.cn\/wp-content\/uploads\/2024\/07\/image-57-325x233.png 325w, http:\/\/www.spiceandwolf.com.cn\/wp-content\/uploads\/2024\/07\/image-57.png 1304w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>JMM\u53ef\u80fd\u5e26\u6765\u53ef\u89c1\u6027\u3001\u539f\u5b50\u6027\u548c\u6709\u5e8f\u6027\u95ee\u9898\u3002<br>\u6240\u8c13\u53ef\u89c1\u6027\uff0c\u5c31\u662f\u67d0\u4e2a\u7ebf\u7a0b\u5bf9\u4e3b\u5185\u5b58\u5185\u5bb9\u7684\u66f4\u6539\uff0c\u5e94\u8be5\u7acb\u523b\u901a\u77e5\u5230\u5176\u4ed6\u7ebf\u7a0b\u3002<br>\u6240\u8c13\u539f\u5b50\u6027\uff0c\u662f\u6307\u4e00\u4e2a\u64cd\u4f5c\u662f\u4e0d\u53ef\u5206\u5272\u7684\uff0c\u4e0d\u80fd\u6267\u884c\u5230\u4e00\u534a\uff0c\u5c31\u4e0d\u6267\u884c\u4e86\u3002<br>\u6240\u8c13\u6709\u5e8f\u6027\uff0c\u5c31\u662f\u6307\u4ee4\u662f\u6709\u5e8f\u7684\uff0c\u4e0d\u4f1a\u88ab\u91cd\u6392\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Volatile\u5173\u952e\u5b57<\/h2>\n\n\n\n<p>volatile\u5173\u952e\u5b57\u662fjava\u63d0\u4f9b\u7684\u4e00\u79cd\u8f7b\u91cf\u7ea7\u540c\u6b65\u673a\u5236\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5b83\u80fd\u591f\u63d0\u4f9b\u53ef\u89c1\u6027\u3002<\/li>\n\n\n\n<li>\u4e0d\u80fd\u4fdd\u8bc1\u539f\u5b50\u6027\u3002<\/li>\n\n\n\n<li>\u7981\u6b62\u6307\u4ee4\u91cd\u6392\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">\u53ef\u89c1\u6027\u6d4b\u8bd5<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>class MyData {\n    volatile int number = 0;\n    public void setTo60() {\n        this.number = 60;\n    }\n}\n\n\/**\n * Volatile\u5173\u952e\u5b57\u7684\u4f7f\u7528\n *\/\npublic class VolatileDemo {\n    public static void main(String&#91;] args) {\n        volatileVisibilityDemo();\n    }\n\n    private static void volatileVisibilityDemo() {\n        System.out.println(\"====\u53ef\u89c1\u6027\u6d4b\u8bd5====\");\n\n        MyData myData = new MyData();\n        new Thread(() -&gt; {\n            System.out.println(Thread.currentThread().getName() + \"\\t \u6267\u884c\");\n\n            try {\n                TimeUnit.SECONDS.sleep(3);\n            } catch (InterruptedException e) {\n                throw new RuntimeException(e);\n            }\n\n            myData.setTo60();\n\n            System.out.println(Thread.currentThread().getName() + \"\\t \u66f4\u65b0number\u503c\" + myData.number);\n        }).start();\n\n        \/\/ main\u7ebf\u7a0b\n        \/\/ \u4fdd\u8bc1\u4e0a\u9762\u7684\u7ebf\u7a0b\u5df2\u7ecf\u6267\u884c\u5b8c\u6bd5\uff0c\u518d\u8fdb\u884cmain\u7ebf\u7a0b\u7684\u6267\u884c\n        while (myData.number == 0) {\n\n        }\n        System.out.println(Thread.currentThread().getName() + \"\/t main\u7ebf\u7a0b\u4e2d\u83b7\u53d6number\u503c\" + myData.number);\n    }\n}<\/code><\/pre>\n\n\n\n<p>MyData\u7c7b\u4e2d\u7684number\u52a0\u4e0avolatile\u548c\u4e0d\u52a0\u662f\u4e24\u79cd\u7ed3\u679c\u3002\u4e0d\u52a0volatile\u65f6\u4e3b\u7ebf\u7a0b\u770b\u4e0d\u5230\u5b50\u7ebf\u7a0b\u4fee\u6539\u7684\u7ed3\u679c\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u539f\u5b50\u6027\u6d4b\u8bd5<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>class MyData {\n    int number = 0;\n\n    public void plusOne() {\n        number++;\n    }\n}\n\npublic class VolatileDemo {\n    public static void main(String&#91;] args) {\n        atomicDemo();\n    }\n\n    \/**\n     * \u539f\u5b50\u6027\u6d4b\u8bd5\n     * \u9700\u6c42\uff1a\u542f\u52a820\u4e2a\u7ebf\u7a0b\uff0c\u6bcf\u4e2a\u7ebf\u7a0b\u6267\u884c1000\u6b21\uff0cnumber++\u64cd\u4f5c\uff0c\u6700\u7ec820\u4e2a\u7ebf\u7a0b\u6267\u884c\u5b8c\u6bd5\u540e\uff0cnumber\u503c\u662f\u591a\u5c11\uff1f\n     *\/\n    private static void atomicDemo() {\n        System.out.println(\"====\u539f\u5b50\u6027\u6d4b\u8bd5====\");\n\n        MyData myData = new MyData();\n\n        for (int i = 1; i &lt;= 20; i++) {\n            new Thread(() -&gt; {\n                for (int j = 0; j &lt; 1000; j++) {\n                    myData.plusOne();\n                }\n            }, String.valueOf(i)).start();\n        }\n\n        while(Thread.activeCount() &gt; 2) {\n            Thread.yield();\n        }\n        System.out.println(Thread.currentThread().getName() + \"\\t main\u7ebf\u7a0b\u4e2d\u83b7\u53d6number\u503c\" + myData.number);\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u8f93\u51fa\u7ed3\u679c\u4e0d\u4e00\u5b9a\u4e3a20000\uff0c\u56e0\u4e3anumber++\u64cd\u4f5c\u5e76\u4e0d\u5177\u6709\u539f\u5b50\u6027\u3002<\/p>\n\n\n\n<p>\u89e3\u51b3\u65b9\u68481:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class MyData {\n    int number = 0;\n\n    public synchronized void plusOne() {\n        number++;\n    }\n}\n\npublic class VolatileDemo {\n    public static void main(String&#91;] args) {\n        atomicDemo();\n    }\n\n    \/**\n     * \u539f\u5b50\u6027\u6d4b\u8bd5\n     * \u9700\u6c42\uff1a\u542f\u52a820\u4e2a\u7ebf\u7a0b\uff0c\u6bcf\u4e2a\u7ebf\u7a0b\u6267\u884c1000\u6b21\uff0cnumber++\u64cd\u4f5c\uff0c\u6700\u7ec820\u4e2a\u7ebf\u7a0b\u6267\u884c\u5b8c\u6bd5\u540e\uff0cnumber\u503c\u662f\u591a\u5c11\uff1f\n     *\/\n    private static void atomicDemo() {\n        System.out.println(\"====\u539f\u5b50\u6027\u6d4b\u8bd5====\");\n\n        MyData myData = new MyData();\n\n        for (int i = 1; i &lt;= 20; i++) {\n            new Thread(() -&gt; {\n                for (int j = 0; j &lt; 1000; j++) {\n                    myData.plusOne();\n                }\n            }, String.valueOf(i)).start();\n        }\n\n        while(Thread.activeCount() &gt; 2) {\n            Thread.yield();\n        }\n        System.out.println(Thread.currentThread().getName() + \"\\t main\u7ebf\u7a0b\u4e2d\u83b7\u53d6number\u503c\" + myData.number);\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u53ef\u4ee5\u901a\u8fc7synchronized\u52a0\u9501\u6765\u4fdd\u8bc1\u65b9\u6cd5\u5185\u64cd\u4f5c\u7684\u539f\u5b50\u6027\uff0c\u4f46\u662f\u8fd9\u79cd\u65b9\u5f0f\u7684\u8fd0\u884c\u6548\u7387\u5f88\u4f4e\u3002<\/p>\n\n\n\n<p>\u89e3\u51b3\u65b9\u68482:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class MyData {\n    AtomicInteger atomicInteger = new AtomicInteger();\n\n    public void atomicPlusOne() {\n        atomicInteger.getAndIncrement();\n    }\n}\n\n\/**\n * Volatile\u5173\u952e\u5b57\u7684\u4f7f\u7528\n *\/\npublic class VolatileDemo {\n    public static void main(String&#91;] args) {\n        atomicDemo();\n    }\n\n    \/**\n     * \u539f\u5b50\u6027\u6d4b\u8bd5\n     * \u9700\u6c42\uff1a\u542f\u52a820\u4e2a\u7ebf\u7a0b\uff0c\u6bcf\u4e2a\u7ebf\u7a0b\u6267\u884c1000\u6b21\uff0cnumber++\u64cd\u4f5c\uff0c\u6700\u7ec820\u4e2a\u7ebf\u7a0b\u6267\u884c\u5b8c\u6bd5\u540e\uff0cnumber\u503c\u662f\u591a\u5c11\uff1f\n     *\/\n    private static void atomicDemo() {\n        System.out.println(\"====\u539f\u5b50\u6027\u6d4b\u8bd5====\");\n\n        MyData myData = new MyData();\n\n        for (int i = 1; i &lt;= 20; i++) {\n            new Thread(() -&gt; {\n                for (int j = 0; j &lt; 1000; j++) {\n                    myData.atomicPlusOne();\n                }\n            }, String.valueOf(i)).start();\n        }\n\n        while(Thread.activeCount() &gt; 2) {\n            Thread.yield();\n        }\n        System.out.println(Thread.currentThread().getName() + \"\\t main\u7ebf\u7a0b\u4e2d\u83b7\u53d6atomic\u4e2d\u7684number\u503c\" + myData.atomicInteger.get());\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u4e5f\u53ef\u4ee5\u901a\u8fc7Atomic\u5bf9\u8c61\u6765\u8fdb\u884c\u9012\u589e\u64cd\u4f5c\uff0c\u8fd9\u79cd\u65b9\u5f0f\u76f8\u8f83\u4e8esynchronized\u65b9\u5f0f\u66f4\u52a0\u9ad8\u6548\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u6709\u5e8f\u6027\u6d4b\u8bd5<\/h4>\n\n\n\n<p>\u8ba1\u7b97\u673a\u5728\u6267\u884c\u7a0b\u5e8f\u65f6\uff0c\u4e3a\u4e86\u63d0\u9ad8\u6027\u80fd\uff0c\u7f16\u8bd1\u5668\u548c\u5904\u7406\u5668\u5e38\u5e38\u4f1a\u5bf9\u6307\u4ee4\u505a\u91cd\u6392\uff0c\u4e00\u822c\u5206\u4e3a\u4ee5\u4e0b\u4e09\u79cd\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"200\" src=\"http:\/\/www.spiceandwolf.com.cn\/wp-content\/uploads\/2024\/07\/image-60-1024x200.png\" alt=\"\" class=\"wp-image-1254\" srcset=\"http:\/\/www.spiceandwolf.com.cn\/wp-content\/uploads\/2024\/07\/image-60-1024x200.png 1024w, http:\/\/www.spiceandwolf.com.cn\/wp-content\/uploads\/2024\/07\/image-60-300x59.png 300w, http:\/\/www.spiceandwolf.com.cn\/wp-content\/uploads\/2024\/07\/image-60-768x150.png 768w, http:\/\/www.spiceandwolf.com.cn\/wp-content\/uploads\/2024\/07\/image-60.png 1424w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>\u5355\u7ebf\u7a0b\u73af\u5883\u91cc\u9762\u786e\u4fdd\u7a0b\u5e8f\u6700\u7ec8\u6267\u884c\u7ed3\u679c\u548c\u4ee3\u7801\u987a\u5e8f\u6267\u884c\u7684\u7ed3\u679c\u4e00\u81f4\uff1b<br>\u5904\u7406\u5668\u5728\u8fdb\u884c\u91cd\u6392\u65f6\u5fc5\u987b\u8981\u8003\u8651\u6307\u4ee4\u4e4b\u95f4\u7684\u6570\u636e\u4f9d\u8d56\u6027\uff1b<br>\u591a\u7ebf\u7a0b\u73af\u5883\u4e2d\u7ebf\u7a0b\u4ea4\u66ff\u6267\u884c\uff0c\u7531\u4e8e\u7f16\u8bd1\u5668\u4f18\u5316\u91cd\u6392\u7684\u5b58\u5728\uff0c\u4e24\u4e2a\u7ebf\u7a0b\u4e2d\u4f7f\u7528\u7684\u53d8\u91cf\u80fd\u5426\u4fdd\u8bc1\u4e00\u81f4\u662f\u65e0\u6cd5\u786e\u5b9a\u7684\uff0c\u7ed3\u679c\u65e0\u6cd5\u9884\u6d4b\u3002<br>volatile\u53ef\u4ee5\u4fdd\u8bc1\u6709\u5e8f\u6027\uff0c\u4e5f\u5c31\u662f\u9632\u6b62\u6307\u4ee4\u91cd\u6392\u3002<br>\u6240\u8c13\u6307\u4ee4\u91cd\u6392\u5e8f\uff0c\u5c31\u662f\u51fa\u4e8e\u4f18\u5316\u8003\u8651\uff0cCPU\u6267\u884c\u6307\u4ee4\u7684\u987a\u5e8f\u66f4\u7a0b\u5e8f\u5458\u81ea\u5df1\u7f16\u5199\u7684\u987a\u5e8f\u4e0d\u4e00\u81f4\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class RestoreSeqDemo {\n    int a = 0;\n    volatile boolean flag = false;\n\n    public void method1() {\n        a = 1;\n        flag = true;\n    }\n\n    public void method2() {\n        if (flag) {\n            a += 5;\n            System.out.println(\"\u6700\u7ec8\u503ca = \" + a);\n        }\n    }\n\n    public static void main(String&#91;] args) {\n        RestoreSeqDemo demo = new RestoreSeqDemo();\n        new Thread(() -&gt; {\n            demo.method1();\n        }, \"ThreadA\").start();\n        new Thread(() -&gt; {\n            demo.method2();\n        }, \"ThreadB\").start();\n    }\n}<\/code><\/pre>\n\n\n\n<p>flag\u53d8\u91cf\u4e0d\u52a0volatile\u4fee\u9970\u65f6\uff0cflag=true\u6307\u4ee4\u53ef\u80fd\u4f1a\u6392\u5728a=1\u6307\u4ee4\u524d\uff0c\u8fd9\u4f1a\u5bfc\u81f4\u51fa\u73b0a\u6700\u7ec8\u8f93\u51fa\u7ed3\u679c\u4e3a5\u62161\u7684\u60c5\u51b5\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u53cc\u7aef\u68c0\u67e5\u9501\uff08\u5355\u4f8b\u6a21\u5f0f\uff09<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>public class SingletonDemo {\n    public static SingletonDemo instance = null;\n\n    private SingletonDemo() {\n        System.out.println(Thread.currentThread().getName() + \"\\t SingletonDemo\u6784\u9020\u65b9\u6cd5\u6267\u884c\u4e86\");\n    }\n\n    public static SingletonDemo getInstance() {\n        if (instance == null) {\n            instance = new SingletonDemo();\n        }\n        return instance;\n    }\n\n    public static void main(String&#91;] args) {\n        \/\/ \u591a\u7ebf\u7a0b\u64cd\u4f5c\n        for (int i = 0; i &lt; 100000; i++) {\n            new Thread(() -&gt; {\n                SingletonDemo.getInstance();\n            }, String.valueOf(i)).start();\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u8fd9\u6bb5\u5355\u4f8b\u6a21\u5f0f\u4ee3\u7801\u660e\u663e\u5b58\u5728\u5e76\u53d1\u95ee\u9898\uff0cgetInstance\u65b9\u6cd5\u5e76\u4e0d\u662f\u539f\u5b50\u64cd\u4f5c\uff0c\u53ef\u80fd\u5b58\u5728\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u8fdb\u5165if\u4ee3\u7801\u5757\u7684\u60c5\u51b5\uff0c\u6240\u4ee5\u9700\u8981\u8fdb\u884c\u4ee3\u7801\u4f18\u5316\uff0c\u4fdd\u8bc1\u53ea\u521b\u5efa\u51fa\u4e00\u4e2a\u5b9e\u4f8b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class SingletonDemo {\n    public static SingletonDemo instance = null;\n\n    private SingletonDemo() {\n        System.out.println(Thread.currentThread().getName() + \"\\t SingletonDemo\u6784\u9020\u65b9\u6cd5\u6267\u884c\u4e86\");\n    }\n\n    public static SingletonDemo getInstance() {\n        if (instance == null) {\n            synchronized (SingletonDemo.class) {\n                if (instance == null) {\n                    instance = new SingletonDemo();\n                }\n            }\n        }\n        return instance;\n    }\n\n    public static void main(String&#91;] args) {\n        \/\/ \u591a\u7ebf\u7a0b\u64cd\u4f5c\n        for (int i = 0; i &lt; 100000; i++) {\n            new Thread(() -&gt; {\n                SingletonDemo.getInstance();\n            }, String.valueOf(i)).start();\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u4f18\u5316\u540e\u540c\u4e00\u65f6\u95f4\u53ea\u80fd\u6709\u4e00\u4e2a\u7ebf\u7a0b\u8fdb\u5165synchronized\u65b9\u6cd5\u4f53\u8fdb\u884c\u65b9\u6cd5\u521b\u5efa\uff0c\u4f46\u662f\u4ecd\u7136\u53ef\u80fd\u51fa\u73b0\u7a7a\u6307\u9488\u7b49\u5f02\u5e38\u60c5\u51b5\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> 0 getstatic #11 &lt;JUC\/SingletonDemo.instance : LJUC\/SingletonDemo;&gt;\n 3 ifnonnull 37 (+34)\n 6 ldc #12 &lt;JUC\/SingletonDemo&gt;\n 8 dup\n 9 astore_0\n10 monitorenter\n11 getstatic #11 &lt;JUC\/SingletonDemo.instance : LJUC\/SingletonDemo;&gt;\n14 ifnonnull 27 (+13)\n17 new #12 &lt;JUC\/SingletonDemo&gt;\n20 dup\n21 invokespecial #13 &lt;JUC\/SingletonDemo.&lt;init&gt; : ()V&gt;\n24 putstatic #11 &lt;JUC\/SingletonDemo.instance : LJUC\/SingletonDemo;&gt;\n27 aload_0\n28 monitorexit\n29 goto 37 (+8)\n32 astore_1\n33 aload_0\n34 monitorexit\n35 aload_1\n36 athrow\n37 getstatic #11 &lt;JUC\/SingletonDemo.instance : LJUC\/SingletonDemo;&gt;\n40 areturn<\/code><\/pre>\n\n\n\n<p>\u8fd9\u662f\u4e0a\u6bb5\u4ee3\u7801\u7684\u5b57\u8282\u7801\uff0c\u5176\u4e2dinstance = new SingletonDemo()\u5bf9\u5e94\u4e8e17\u300120\u300121\u300124\u884c\uff0c\u5b57\u8282\u7801\u6307\u4ee4\u7684\u4f5c\u7528\u5206\u522b\u662f\uff1a<br>1. new\uff0c\u5728\u5185\u5b58\u4e2d\u4e3a\u65b0\u521b\u5efa\u7684\u5bf9\u8c61\u5f00\u8f9f\u7a7a\u95f4\uff0c\u5982\u679c\u9996\u6b21\u52a0\u8f7d\u7c7b\u5219\u8fdb\u884c\u7c7b\u52a0\u8f7d\u3002<br>2. dup\uff0c\u8d4b\u503c\u64cd\u4f5c\u6570\u6808\u6808\u9876\u6570\u636e\u5e76\u538b\u5165\u6808\u9876\u3002<br>3. invokespecial\uff0c\u8c03\u7528\u6784\u9020\u65b9\u6cd5\u8fdb\u884c\u521d\u59cb\u5316\u3002<br>4. putstatic\uff0c\u5c06\u521b\u5efa\u7684\u5bf9\u8c61\u5730\u5740\u8d4b\u503c\u7ed9\u9759\u6001\u53d8\u91cfinstance\u3002<br>\u56e0\u4e3a\u6307\u4ee4\u91cd\u6392\u7684\u539f\u56e0\uff0c3\u30014\u6b65\u7684\u64cd\u4f5c\u53ef\u80fd\u4f1a\u98a0\u5012\u987a\u5e8f\uff0c\u6240\u4ee5\uff0c\u53ef\u80fd\u5b58\u5728\u5bf9\u8c61\u5f15\u7528\u5df2\u7ecf\u6709\u503c\uff0c\u4f46\u662f\u5e76\u6ca1\u6709\u88ab\u521d\u59cb\u5316\u7684\u65f6\u523b\uff08\u6570\u636e\u4e3a\u4e71\u7801\uff09\uff0c\u8fd9\u65f6\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u62ff\u5230\u5bf9\u8c61\u4f7f\u7528\u4f1a\u51fa\u73b0\u7a7a\u6307\u9488\u7b49\u95ee\u9898\u3002<\/p>\n\n\n\n<p>\u89e3\u51b3\u65b9\u6cd5\uff08\u7528volatile\u4fee\u9970instance\u53d8\u91cf\uff0c\u9632\u6b62\u6307\u4ee4\u91cd\u6392\uff09\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class SingletonDemo {\n    public static volatile SingletonDemo instance = null;\n\n    private SingletonDemo() {\n        System.out.println(Thread.currentThread().getName() + \"\\t SingletonDemo\u6784\u9020\u65b9\u6cd5\u6267\u884c\u4e86\");\n    }\n\n    public static SingletonDemo getInstance() {\n        if (instance == null) {\n            synchronized (SingletonDemo.class) {\n                if (instance == null) {\n                    instance = new SingletonDemo();\n                }\n            }\n        }\n        return instance;\n    }\n\n    public static void main(String&#91;] args) {\n        \/\/ \u591a\u7ebf\u7a0b\u64cd\u4f5c\n        for (int i = 0; i &lt; 100000; i++) {\n            new Thread(() -&gt; {\n                SingletonDemo.getInstance();\n            }, String.valueOf(i)).start();\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">CAS<\/h2>\n\n\n\n<p>CAS\u662fCPU\u7684\u4e00\u6761\u539f\u5b50\u6307\u4ee4\uff08cmpxchg\u6307\u4ee4\uff09\uff0c\u56e0\u4e3a\u5b83\u662f\u4e00\u6761\u539f\u5b50\u6307\u4ee4\uff0c\u6240\u4ee5\u5f53\u6761\u6307\u4ee4\u6267\u884c\u4e0d\u4f1a\u9020\u6210\u4e0d\u4e00\u81f4\u6027\u3002\u6307\u4ee4\u903b\u8f91\u662f\u901a\u8fc7\u5185\u5b58\u5730\u5740\u83b7\u53d6\u5f53\u524d\u503c\u540e\uff0c\u548c\u9884\u671f\u65e7\u503c\u8fdb\u884c\u6bd4\u8f83\uff0c\u5982\u679c\u76f8\u7b49\uff0c\u5c31\u5c06\u5bf9\u5e94\u5185\u5b58\u5730\u5740\u7684\u503c\u6539\u4e3a\u9884\u671f\u503c\uff0c\u5982\u679c\u4e0d\u76f8\u7b49\u5219\u4ec0\u4e48\u90fd\u4e0d\u505a\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">CAS\u5e95\u5c42\u539f\u7406<\/h4>\n\n\n\n<p>\u6211\u4eec\u4ee5AtomicInteger\u4e3a\u7684getAndIncrement\u65b9\u6cd5\u4e3a\u4f8b\uff0c\u5206\u6790\u4e00\u4e0b\u539f\u5b50\u7c7b\u662f\u5982\u4f55\u4f7f\u7528CAS\u65b9\u6cd5\u5b9e\u73b0\u6574\u578b\u9012\u589e\u64cd\u4f5c\u7684\u539f\u5b50\u6027\u7684\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    public final int getAndIncrement() {\n        return unsafe.getAndAddInt(this, valueOffset, 1);\n    }<\/code><\/pre>\n\n\n\n<p>getAndIncrement\u65b9\u6cd5\u4f53\u5185\u8c03\u7528\u7684\u662fUnsafe\u7c7b\u7684getAndAddInt\u65b9\u6cd5\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    public final int getAndAddInt(Object var1, long var2, int var4) {\n        int var5;\n        do {\n            var5 = this.getIntVolatile(var1, var2);\n        } while(!this.compareAndSwapInt(var1, var2, var5, var5 + var4));\n\n        return var5;\n    }<\/code><\/pre>\n\n\n\n<p>\u4ece\u4e0a\u9762\u7684\u65b9\u6cd5\u4f20\u9012\u53ef\u4ee5\u770b\u51fa\uff0cvar1\u662f\u5bf9\u8c61\u5730\u5740\uff0c\u800cvar2\u662fvalue\u503c\u7684\u5730\u5740\u504f\u79fb\u91cf\uff0cvar4\u662f\u9012\u589e\u91cf\uff0c\u8be5\u65b9\u6cd5\u5148\u901a\u8fc7getIntVolatile\u65b9\u6cd5\uff0c\u62ff\u5230\u5f53\u524d\u65f6\u95f4\u70b9\u5171\u4eab\u53d8\u91cf\u7684\u503c\uff0c\u7136\u540e\u57fa\u4e8ecompareAndSwapInt\u65b9\u6cd5\u7684\u8fd4\u56de\u503c\u8fdb\u884c\u5faa\u73af\u5224\u65ad\uff0c\u6211\u4eec\u53ef\u4ee5\u518d\u6765\u770b\u4e0bcompareAndSwapInt\u65b9\u6cd5\u7684\u903b\u8f91\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public final native boolean compareAndSwapInt(Object var1, long var2, int var4, int var5);<\/code><\/pre>\n\n\n\n<p>\u53ef\u60dc\u8fd9\u662f\u672c\u5730\u65b9\u6cd5\uff0c\u662f\u57fa\u4e8e\u5176\u4ed6\u8bed\u8a00(C\u8bed\u8a00)\u5b9e\u73b0\u7684java\u8c03\u7528\u7684\uff0c\u8981\u5f7b\u5e95\u641e\u6e05\u695a\uff0c\u6211\u4eec\u9700\u8981\u518d\u770b\u770b\u8fd9\u4e2a\u65b9\u6cd5\u7684C\u8bed\u8a00\u5b9e\u73b0\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>UNSAFE_ENTRY(Boolean, Unsafe_CompareAndSwapInt(JNIEnv *env, object unsafe, long obj, long offset, jint e, jint x))\n    UnsafeWrapper(\"Unsafe_CompareAndSwapInt\");\n    oop p = JNIHandles::resolve(obj);\n    jint* addr = (jint *) index_oop_from_field_offset_long(p, offset);\n    return (jint)(Atomic::cmpxchg(x, addr, e)) == e;\nUNSAFE_END<\/code><\/pre>\n\n\n\n<p>\u8fd9\u91ccobj\u662f\u5bf9\u8c61\u5730\u5740\uff0coffset\u662fvalue\u504f\u79fb\u91cf\uff0ce\u662f\u65e7\u503c\uff0cx\u662f\u671f\u671b\u503c\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">CAS\u7f3a\u70b9<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Java\u4e2d\u5f88\u591aCAS\u65b9\u6cd5\u90fd\u6709\u81ea\u65cb\u9501\u903b\u8f91\uff0c\u4e00\u76f4\u5faa\u73af\uff0c\u5f00\u9500\u6bd4\u8f83\u5927\u3002\u6211\u4eec\u53ef\u4ee5\u770b\u5230getAndAddInt\u65b9\u6cd5\u6267\u884c\u65f6\uff0c\u6709\u4e2ado while\uff0c\u5982\u679cCAS\u5931\u8d25\uff0c\u4f1a\u4e00\u76f4\u8fdb\u884c\u5c1d\u8bd5\u3002\u5982\u679cCAS\u957f\u65f6\u95f4\u4e00\u76f4\u4e0d\u6210\u529f\uff0c\u53ef\u80fd\u4f1a\u7ed9CPU\u5e26\u6765\u5f88\u5927\u7684\u5f00\u9500\u3002<\/li>\n\n\n\n<li>\u5bf9\u4e00\u4e2a\u5171\u4eab\u53d8\u91cf\u6267\u884c\u64cd\u4f5c\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5faa\u73afCAS\u7684\u65b9\u5f0f\u6765\u4fdd\u8bc1\u539f\u5b50\u64cd\u4f5c\uff0c\u4f46\u662f\u5bf9\u591a\u4e2a\u5171\u4eab\u53d8\u91cf\u64cd\u4f5c\u65f6\uff0c\u5faa\u73afCAS\u5c31\u65e0\u6cd5\u4fdd\u8bc1\u64cd\u4f5c\u7684\u539f\u5b50\u6027\uff0c\u8fd9\u4e2a\u65f6\u5019\u5c31\u53ef\u4ee5\u7528\u9501\u6765\u4fdd\u8bc1\u539f\u5b50\u6027\u3002<\/li>\n\n\n\n<li>\u5f15\u51fa\u4e86ABA\u95ee\u9898\u3002<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">CAS\u7684ABA\u95ee\u9898<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>public class ABADemo {\n    public static void main(String&#91;] args) {\n        AtomicReference&lt;Integer&gt; atomicReference = new AtomicReference&lt;&gt;(100);\n        demo01(atomicReference);\n    }\n\n    private static void demo01(AtomicReference&lt;Integer&gt; atomicReference) {\n        System.out.println(\"====ABA\u95ee\u9898\u4ea7\u751f====\");\n\n        new Thread(() -&gt; {\n            try {\n                TimeUnit.SECONDS.sleep(2);\n            } catch (InterruptedException e) {\n                throw new RuntimeException(e);\n            }\n            System.out.println(atomicReference.compareAndSet(100, 2000) + \"\\t\" + atomicReference.get());\n        }, \"ThreadA\").start();\n\n        new Thread(() -&gt; {\n            \/\/ CAS\u64cd\u4f5c\n            System.out.println(atomicReference.compareAndSet(100, 111));\n            \/\/ CAS\u64cd\u4f5c\n            System.out.println(atomicReference.compareAndSet(111, 100));\n        }, \"ThreadB\").start();\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u521d\u59cb\u503c100\uff0c\u5f53ThreadB\u7ebf\u7a0b\u5c06\u503c\u6539\u4e3a111\u5e76\u6539\u56de\u6765\u65f6\uff0cThreadA\u7684CAS\u64cd\u4f5c\u90fd\u80fd\u6210\u529f\uff0c\u8fd9\u4e2a\u8fc7\u7a0b\u4e2d\u539f\u5b50\u5f15\u7528\u4e2d\u7684\u503c\u5b9e\u9645\u5df2\u7ecf\u53d1\u751f\u4e86\u53d8\u5316\uff0c\u53ea\u4e0d\u8fc7\u7ecf\u5386\u4e86\u4e24\u6b21\u4fee\u6539\u540e\uff0c\u503c\u53c8\u56de\u5230\u4e86100\uff0c\u4f46\u90e8\u5206\u573a\u666f\u4e0b\u6211\u4eec\u53ea\u5e0c\u671b\u7a0b\u5e8f\u57fa\u4e8e\u6700\u521d\u7684100\u8fdb\u884cCAS\u64cd\u4f5c\uff0c\u90a3\u5982\u4f55\u533a\u522b\u6700\u521d\u7684100\u548c\u7ecf\u8fc7\u4fee\u6539\u540e\u7684100\uff1f\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u7248\u672c\u53f7\u5b9e\u73b0\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class ABADemo {\n    public static void main(String&#91;] args) {\n        AtomicStampedReference&lt;Integer&gt; atomicStampedReference = new AtomicStampedReference&lt;&gt;(100, 1);\n        demo02(atomicStampedReference);\n    }\n\n    private static void demo02(AtomicStampedReference&lt;Integer&gt; atomicStampedReference) {\n        System.out.println(\"====ABA\u95ee\u9898\u7684\u89e3\u51b3====\");\n\n        new Thread(() -&gt; {\n            int stamp = atomicStampedReference.getStamp();\n            System.out.println(\"ThreadA====\u7b2c\u4e00\u6b21\u83b7\u53d6\u7684\u7248\u672c\u53f7====\" + stamp);\n            System.out.println(\"ThreadB====\u7b2c\u4e00\u6b21\u83b7\u53d6\u7684\u6570\u636e====\" + atomicStampedReference.getReference());\n\n            try {\n                TimeUnit.SECONDS.sleep(4);\n            } catch (InterruptedException e) {\n                throw new RuntimeException(e);\n            }\n\n            boolean result = atomicStampedReference.compareAndSet(\n                    100,\n                    2000,\n                    stamp,\n                    stamp + 1\n            );\n            System.out.println(\"CAS\u6bd4\u8f83\u7684\u7ed3\u679cresult====\" + result);\n            System.out.println(\"ThreadA====\u7b2c\u4e8c\u6b21\u83b7\u53d6\u7684\u7248\u672c\u53f7====\" + stamp);\n            System.out.println(\"ThreadB====\u7b2c\u4e8c\u6b21\u83b7\u53d6\u7684\u6570\u636e====\" + atomicStampedReference.getReference());\n        }, \"ThreadA\").start();\n\n        new Thread(() -&gt; {\n            System.out.println(\"ThreadA====\u7b2c\u4e00\u6b21\u83b7\u53d6\u7684\u7248\u672c\u53f7====\" + atomicStampedReference.getStamp());\n            System.out.println(\"ThreadB====\u7b2c\u4e00\u6b21\u83b7\u53d6\u7684\u6570\u636e====\" + atomicStampedReference.getReference());\n\n            try {\n                TimeUnit.SECONDS.sleep(1);\n            } catch (InterruptedException e) {\n                throw new RuntimeException(e);\n            }\n\n            atomicStampedReference.compareAndSet(\n                    100,\n                    111,\n                    atomicStampedReference.getStamp(),\n                    atomicStampedReference.getStamp() + 1\n            );\n            System.out.println(\"ThreadA====\u7b2c\u4e8c\u6b21\u83b7\u53d6\u7684\u7248\u672c\u53f7====\" + atomicStampedReference.getStamp());\n            System.out.println(\"ThreadB====\u7b2c\u4e8c\u6b21\u83b7\u53d6\u7684\u6570\u636e====\" + atomicStampedReference.getReference());\n\n            atomicStampedReference.compareAndSet(\n                    111,\n                    100,\n                    atomicStampedReference.getStamp(),\n                    atomicStampedReference.getStamp() + 1\n            );\n            System.out.println(\"ThreadA====\u7b2c\u4e09\u6b21\u83b7\u53d6\u7684\u7248\u672c\u53f7====\" + atomicStampedReference.getStamp());\n            System.out.println(\"ThreadB====\u7b2c\u4e09\u6b21\u83b7\u53d6\u7684\u6570\u636e====\" + atomicStampedReference.getReference());\n        }, \"ThreadB\").start();\n\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u8fd9\u91ccAtomicStampedReference\u7c7b\u53ef\u4ee5\u4fdd\u5b58\u65f6\u95f4\u6233\u5c06\u5176\u4f5c\u4e3a\u7248\u672c\u53f7\u6765\u533a\u522b\u4e0d\u540c\u65f6\u95f4\u6bb5\u4f46\u5177\u6709\u76f8\u540c\u503c\u7684\u5bf9\u8c61\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u96c6\u5408\u7c7b\u4e0d\u5b89\u5168\u95ee\u9898<\/h2>\n\n\n\n<p>\u591a\u7ebf\u7a0b\u540c\u65f6\u5bf9\u96c6\u5408\u8fdb\u884c\u64cd\u4f5c\u65f6\u4f1a\u51fa\u73b0ConcurrentModificationException\u5f02\u5e38\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class ListNoSafeDemo {\n    public static void main(String&#91;] args) {\n\/\/        listDemo();\n\/\/        setDemo();\n        mapDemo();\n    }\n\n    public static void mapDemo() {\n        Map&lt;String, String&gt; map = new HashMap&lt;&gt;();\n        for (int i = 1; i &lt;= 10; i++) {\n            new Thread(() -&gt; {\n                map.put(UUID.randomUUID().toString().substring(0, 8), UUID.randomUUID().toString().substring(0, 8));\n\n                System.out.println(Thread.currentThread().getName() + \"\\t\" + map);\n            }, String.valueOf(i)).start();\n        }\n    }\n\n    public static void setDemo() {\n        HashSet&lt;String&gt; list = new HashSet&lt;&gt;();\n        for (int i = 1; i &lt;= 10; i++) {\n            new Thread(() -&gt; {\n                list.add(UUID.randomUUID().toString().substring(0, 8));\n\n                System.out.println(Thread.currentThread().getName() + \"\\t\" + list);\n            }, String.valueOf(i)).start();\n        }\n    }\n\n    public static void listDemo() {\n        List&lt;String&gt; list = new ArrayList&lt;&gt;();\n        for (int i = 1; i &lt;= 10; i++) {\n            new Thread(() -&gt; {\n                list.add(UUID.randomUUID().toString().substring(0, 8));\n\n                System.out.println(Thread.currentThread().getName() + \"\\t\" + list);\n            }, String.valueOf(i)).start();\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u53ef\u4ee5\u4f7f\u7528\u5e76\u53d1\u5305\u4e2d\u7684\u96c6\u5408\u7c7b\u8fdb\u884c\u64cd\u4f5c\u6765\u89e3\u51b3\u8fd9\u4e2a\u5e76\u53d1\u95ee\u9898\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class ListNoSafeDemo {\n    public static void main(String&#91;] args) {\n\/\/        listDemo();\n\/\/        setDemo();\n        mapDemo();\n    }\n\n    public static void mapDemo() {\n        Map&lt;String, String&gt; map = new ConcurrentHashMap&lt;&gt;();\n        for (int i = 1; i &lt;= 10; i++) {\n            new Thread(() -&gt; {\n                map.put(UUID.randomUUID().toString().substring(0, 8), UUID.randomUUID().toString().substring(0, 8));\n\n                System.out.println(Thread.currentThread().getName() + \"\\t\" + map);\n            }, String.valueOf(i)).start();\n        }\n    }\n\n    public static void setDemo() {\n        Set&lt;String&gt; list = new CopyOnWriteArraySet&lt;&gt;();\n        for (int i = 1; i &lt;= 10; i++) {\n            new Thread(() -&gt; {\n                list.add(UUID.randomUUID().toString().substring(0, 8));\n\n                System.out.println(Thread.currentThread().getName() + \"\\t\" + list);\n            }, String.valueOf(i)).start();\n        }\n    }\n\n    public static void listDemo() {\n        List&lt;String&gt; list = new CopyOnWriteArrayList&lt;&gt;();\n        for (int i = 1; i &lt;= 10; i++) {\n            new Thread(() -&gt; {\n                list.add(UUID.randomUUID().toString().substring(0, 8));\n\n                System.out.println(Thread.currentThread().getName() + \"\\t\" + list);\n            }, String.valueOf(i)).start();\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u9501\u76f8\u5173<\/h2>\n\n\n\n<h4 class=\"wp-block-heading\">Synchronized<\/h4>\n\n\n\n<p>\u4ee5\u4e0b\u9762\u4ee3\u7801\u4e3a\u4f8b\uff0c\u4ece\u5404\u884c\u4e3a\u4e2d\u63a2\u8ba8synchronized\u5173\u952e\u5b57\u7684\u52a0\u9501\u7279\u6027\uff1a<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">\u6848\u4f8b1<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>class Phone {\n    public synchronized void sendEmail() {\n        System.out.println(\"sendEmail...\");\n    }\n\n    public synchronized void sendMessage() {\n        System.out.println(\"sendMessage...\");\n    }\n}\n\n\/**\n * \u5148\u6253\u5370\u90ae\u4ef6\u8fd8\u662f\u5148\u6253\u5370\u77ed\u4fe1\n *\/\npublic class LockDemo {\n    public static void main(String&#91;] args) {\n        Phone phone = new Phone();\n\n        new Thread(() -&gt; {\n            phone.sendEmail();\n        }, \"A\").start();\n\n        try {\n            TimeUnit.SECONDS.sleep(1);\n        } catch (InterruptedException e) {\n            throw new RuntimeException(e);\n        }\n\n        new Thread(() -&gt; {\n            phone.sendMessage();\n        }, \"B\").start();\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u573a\u666f\uff1a\u5355\u4e2a\u5bf9\u8c61\u7684\u6807\u51c6\u52a0\u9501\u8c03\u7528\u3002<br>\u7ed3\u679c\uff1a\u5148\u6253\u5370\u90ae\u4ef6\uff0c\u518d\u6253\u5370\u6d88\u606f\u3002<br>\u539f\u56e0\uff1asynchronized\u5173\u952e\u5b57\u57fa\u4e8e\u5b9e\u4f8b\u5bf9\u8c61\u8fdb\u884c\u52a0\u9501\uff0c\u6240\u4ee5A\u8c03\u7528sendEmail\u65f6B\u8c03\u7528\u7684sendMessage\u4f1a\u88ab\u9501\u4f4f\u3002<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">\u6848\u4f8b2<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>class Phone {\n    public synchronized void sendEmail() {\n        try {\n            TimeUnit.SECONDS.sleep(3);\n        } catch (InterruptedException e) {\n            throw new RuntimeException(e);\n        }\n        System.out.println(\"sendEmail...\");\n    }\n\n    public synchronized void sendMessage() {\n        System.out.println(\"sendMessage...\");\n    }\n}\n\n\/**\n * \u5148\u6253\u5370\u90ae\u4ef6\u8fd8\u662f\u5148\u6253\u5370\u77ed\u4fe1\n *\/\npublic class LockDemo {\n    public static void main(String&#91;] args) {\n        Phone phone = new Phone();\n\n        new Thread(() -&gt; {\n            phone.sendEmail();\n        }, \"A\").start();\n\n        try {\n            TimeUnit.SECONDS.sleep(1);\n        } catch (InterruptedException e) {\n            throw new RuntimeException(e);\n        }\n\n        new Thread(() -&gt; {\n            phone.sendMessage();\n        }, \"B\").start();\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u573a\u666f\uff1a\u5728Email\u65b9\u6cd5\u4e2d\u52a0\u5165\u4f11\u7720\u4ee3\u7801\uff0c\u4ecd\u7136\u662f\u5355\u4e2a\u5bf9\u8c61\u7684\u52a0\u9501\u8c03\u7528\u3002<br>\u7ed3\u679c\uff1a\u5148\u6253\u5370\u90ae\u4ef6\uff0c\u518d\u6253\u5370\u6d88\u606f\u3002<br>\u539f\u56e0\uff1asynchronized\u5173\u952e\u5b57\u57fa\u4e8e\u5b9e\u4f8b\u5bf9\u8c61\u8fdb\u884c\u52a0\u9501\uff0c\u6240\u4ee5A\u8c03\u7528sendEmail\u65f6B\u8c03\u7528\u7684sendMessage\u4f1a\u88ab\u9501\u4f4f\uff0c\u8fd9\u65f6\u5728sendEmail\u4e2d\u4e0d\u7ba1\u505c\u987f\u591a\u4e45\uff0c\u53ea\u8981\u6ca1\u6709\u89e3\u9501\uff0cB\u7ebf\u7a0b\u7684sendMessage\u5c31\u9700\u8981\u4e00\u76f4\u7b49\u5f85\u3002<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">\u6848\u4f8b3:<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>class Phone {\n    public synchronized void sendEmail() {\n        try {\n            TimeUnit.SECONDS.sleep(3);\n        } catch (InterruptedException e) {\n            throw new RuntimeException(e);\n        }\n        System.out.println(\"sendEmail...\");\n    }\n\n    public synchronized void sendMessage() {\n        System.out.println(\"sendMessage...\");\n    }\n\n    public void hello() {\n        System.out.println(\"hello...\");\n    }\n}\n\n\/**\n * \u5148\u6253\u5370\u90ae\u4ef6\u8fd8\u662f\u5148\u6253\u5370\u77ed\u4fe1\n *\/\npublic class LockDemo {\n    public static void main(String&#91;] args) {\n        Phone phone = new Phone();\n\n        new Thread(() -&gt; {\n            phone.sendEmail();\n        }, \"A\").start();\n\n        try {\n            TimeUnit.SECONDS.sleep(1);\n        } catch (InterruptedException e) {\n            throw new RuntimeException(e);\n        }\n\n        new Thread(() -&gt; {\n            phone.sendMessage();\n        }, \"B\").start();\n\n        new Thread(() -&gt; {\n            phone.hello();\n        }, \"C\").start();    \n    }\n}<\/code><\/pre>\n\n\n\n<p>\u573a\u666f\uff1a\u589e\u52a0\u666e\u901a\u65b9\u6cd5\u3002<br>\u7ed3\u679c\uff1a\u5148\u6253\u5370\u666e\u901a\u65b9\u6cd5\u3002<br>\u539f\u56e0\uff1a\u666e\u901a\u65b9\u6cd5\u5e76\u6ca1\u6709\u52a0\u9501\uff0c\u76f4\u63a5\u6253\u5370\u51fa\u6765\u4e86\u3002<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">\u6848\u4f8b4<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>class Phone {\n    public synchronized void sendEmail() {\n        try {\n            TimeUnit.SECONDS.sleep(3);\n        } catch (InterruptedException e) {\n            throw new RuntimeException(e);\n        }\n        System.out.println(\"sendEmail...\");\n    }\n\n    public synchronized void sendMessage() {\n        System.out.println(\"sendMessage...\");\n    }\n\n    public void hello() {\n        System.out.println(\"hello...\");\n    }\n}\n\n\/**\n * \u5148\u6253\u5370\u90ae\u4ef6\u8fd8\u662f\u5148\u6253\u5370\u77ed\u4fe1\n *\/\npublic class LockDemo {\n    public static void main(String&#91;] args) {\n        Phone phone = new Phone();\n\n        new Thread(() -&gt; {\n            phone.sendEmail();\n        }, \"A\").start();\n\n        try {\n            TimeUnit.SECONDS.sleep(1);\n        } catch (InterruptedException e) {\n            throw new RuntimeException(e);\n        }\n\n        new Thread(() -&gt; {\n            phone.sendMessage();\n        }, \"B\").start();\n\n        new Thread(() -&gt; {\n            phone.hello();\n        }, \"C\").start();    \n    }\n}<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">\u6848\u4f8b5<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>class Phone {\n    public synchronized void sendEmail() {\n        try {\n            TimeUnit.SECONDS.sleep(3);\n        } catch (InterruptedException e) {\n            throw new RuntimeException(e);\n        }\n        System.out.println(\"sendEmail...\");\n    }\n\n    public synchronized void sendMessage() {\n        System.out.println(\"sendMessage...\");\n    }\n\n    public void hello() {\n        System.out.println(\"hello...\");\n    }\n}\n\n\/**\n * \u5148\u6253\u5370\u90ae\u4ef6\u8fd8\u662f\u5148\u6253\u5370\u77ed\u4fe1\n *\/\npublic class LockDemo {\n    public static void main(String&#91;] args) {\n        Phone phone1 = new Phone();\n        Phone phone2 = new Phone();\n\n        new Thread(() -&gt; {\n            phone1.sendEmail();\n        }, \"A\").start();\n\n        try {\n            TimeUnit.SECONDS.sleep(1);\n        } catch (InterruptedException e) {\n            throw new RuntimeException(e);\n        }\n\n        new Thread(() -&gt; {\n            phone2.sendMessage();\n        }, \"B\").start();\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u573a\u666f\uff1a\u4e24\u4e2a\u5bf9\u8c61\u5206\u522b\u8c03\u7528\u4e24\u4e2asynchronized\u4fee\u9970\u7684\u65b9\u6cd5\u3002<br>\u7ed3\u679c\uff1a\u6d88\u606f\u5148\u6253\u5370\uff0c\u90ae\u4ef6\u540e\u6253\u5370\u3002<br>\u539f\u56e0\uff1a\u4e24\u4e2a\u5bf9\u8c61\uff0c\u6240\u4ee5\u6709\u4e24\u4e2a\u9501\uff0c\u65b9\u6cd5\u6267\u884c\u65f6\u4e92\u4e0d\u5f71\u54cd\uff0c\u6240\u4ee5\u65b9\u6cd5\u4f53\u91cd\u6267\u884c\u903b\u8f91\u66f4\u5feb\u7684\u6d88\u606f\u65b9\u6cd5\u66f4\u5feb\u6253\u5370\u3002<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">\u6848\u4f8b6<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>class Phone {\n    public static synchronized void sendEmail() {\n        try {\n            TimeUnit.SECONDS.sleep(3);\n        } catch (InterruptedException e) {\n            throw new RuntimeException(e);\n        }\n        System.out.println(\"sendEmail...\");\n    }\n\n    public static synchronized void sendMessage() {\n        System.out.println(\"sendMessage...\");\n    }\n\n    public void hello() {\n        System.out.println(\"hello...\");\n    }\n}\n\n\/**\n * \u5148\u6253\u5370\u90ae\u4ef6\u8fd8\u662f\u5148\u6253\u5370\u77ed\u4fe1\n *\/\npublic class LockDemo {\n    public static void main(String&#91;] args) {\n        Phone phone1 = new Phone();\n        Phone phone2 = new Phone();\n\n        new Thread(() -&gt; {\n            phone1.sendEmail();\n        }, \"A\").start();\n\n        try {\n            TimeUnit.SECONDS.sleep(1);\n        } catch (InterruptedException e) {\n            throw new RuntimeException(e);\n        }\n\n        new Thread(() -&gt; {\n            phone2.sendMessage();\n        }, \"B\").start();\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u573a\u666f\uff1a\u4e24\u4e2a\u65b9\u6cd5\u662fsynchronized\u4fee\u9970\u7684\u9759\u6001\u65b9\u6cd5\uff0c\u5e76\u4e14\u6709\u4e24\u4e2a\u5bf9\u8c61\u5206\u522b\u8c03\u7528\u8fd9\u4e24\u4e2a\u65b9\u6cd5\u3002<br>\u7ed3\u679c\uff1a\u90ae\u4ef6\u5148\u6253\u5370\uff0c\u6d88\u606f\u540e\u6253\u5370\u3002<br>\u539f\u56e0\uff1a\u9759\u6001\u65b9\u6cd5\u662f\u5bf9\u7c7b\u7684\u5b57\u8282\u7801\u6587\u4ef6\u8fdb\u884c\u52a0\u9501\u7684\uff0c\u6240\u4ee5\u4e24\u4e2a\u5bf9\u8c61\u8c03\u7528\u7684\u65b9\u6cd5\u5b9e\u8d28\u662f\u57fa\u4e8e\u540c\u4e00\u4e2a\u7c7b\u5b57\u8282\u7801\u6587\u4ef6\u8fdb\u884c\u52a0\u9501\u5904\u7406\uff0c\u6240\u4ee5\u5148\u62ff\u5230\u9501\u7684\u90ae\u4ef6\u5148\u6253\u5370\u5b8c\u540e\u89e3\u9501\u540e\u6d88\u606f\u65b9\u6cd5\u624d\u4f1a\u6267\u884c\u3002<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">\u6848\u4f8b7<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>class Phone {\n    public synchronized void sendEmail() {\n        try {\n            TimeUnit.SECONDS.sleep(3);\n        } catch (InterruptedException e) {\n            throw new RuntimeException(e);\n        }\n        System.out.println(\"sendEmail...\");\n    }\n\n    public static synchronized void sendMessage() {\n        System.out.println(\"sendMessage...\");\n    }\n\n    public void hello() {\n        System.out.println(\"hello...\");\n    }\n}\n\n\/**\n * \u5148\u6253\u5370\u90ae\u4ef6\u8fd8\u662f\u5148\u6253\u5370\u77ed\u4fe1\n *\/\npublic class LockDemo {\n    public static void main(String&#91;] args) {\n        Phone phone1 = new Phone();\n        Phone phone2 = new Phone();\n\n        new Thread(() -&gt; {\n            phone1.sendEmail();\n        }, \"A\").start();\n\n        try {\n            TimeUnit.SECONDS.sleep(1);\n        } catch (InterruptedException e) {\n            throw new RuntimeException(e);\n        }\n\n        new Thread(() -&gt; {\n            phone2.sendMessage();\n        }, \"B\").start();\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u573a\u666f\uff1a\u53ea\u6709\u90ae\u4ef6\u65b9\u6cd5\u662f\u9759\u6001\u540c\u6b65\u65b9\u6cd5\uff0c\u6d88\u606f\u65b9\u6cd5\u4ec5\u662f\u540c\u6b65\u65b9\u6cd5\uff0c\u540c\u4e00\u4e2a\u5bf9\u8c61\u8c03\u7528\u4e24\u4e2a\u65b9\u6cd5\u3002<br>\u7ed3\u679c\uff1a\u6d88\u606f\u65b9\u6cd5\u5148\u6253\u5370\uff0c\u90ae\u4ef6\u65b9\u6cd5\u540e\u6253\u5370\u3002<br>\u539f\u56e0\uff1a\u9759\u6001\u540c\u6b65\u65b9\u6cd5\u548c\u975e\u9759\u6001\u540c\u6b65\u65b9\u6cd5\u4f7f\u7528\u7684\u662f\u4e24\u4e2a\u9501\uff0c\u4e00\u4e2a\u662f\u7c7b\u5b57\u8282\u7801\u6587\u4ef6\u4f5c\u4e3a\u9501\uff0c\u4e00\u4e2a\u662f\u5bf9\u8c61\u4f5c\u4e3a\u9501\uff0c\u4e92\u4e0d\u5f71\u54cd\uff0c\u6240\u4ee5\u65b9\u6cd5\u4f53\u4e2d\u6267\u884c\u903b\u8f91\u66f4\u5feb\u7684\u5148\u6253\u5370\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u516c\u5e73\u9501&amp;\u975e\u516c\u5e73\u9501<\/h2>\n\n\n\n<h2 class=\"wp-block-heading\">\u53ef\u91cd\u5165\u9501<\/h2>\n\n\n\n<p>\u53ef\u91cd\u5165\u9501\u53c8\u53eb\u9012\u5f52\u9501\uff0c\u6307\u7684\u540c\u4e00\u4e2a\u7ebf\u7a0b\u5728\u5916\u5c42\u65b9\u6cd5\u83b7\u5f97\u9501\u65f6\uff0c\u8fdb\u5165\u5185\u5c42\u65b9\u6cd5\u4f1a\u81ea\u52a8\u83b7\u53d6\u9501\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u7ebf\u7a0b\u53ef\u4ee5\u8fdb\u5165\u4efb\u4f55\u4e00\u4e2a\u5b83\u5df2\u7ecf\u62e5\u6709\u9501\u7684\u4ee3\u7801\u5757\u3002\u6bd4\u5982method01\u65b9\u6cd5\u91cc\u9762\u6709method2\u65b9\u6cd5\uff0c\u4e24\u4e2a\u65b9\u6cd5\u90fd\u6709\u540c\u4e00\u628a\u9501\uff0c\u5f97\u5230\u4e86method01\u7684\u9501\uff0c\u5c31\u81ea\u52a8\u5f97\u5230\u4e86method02\u7684\u9501\u3002<br>\u5c31\u50cf\u6709\u4e86\u5bb6\u95e8\u7684\u9501\uff0c\u5395\u6240\u3001\u4e66\u623f\u3001\u53a8\u623f\u5c31\u4e3a\u4e86\u4f60\u655e\u5f00\u4e86\u4e00\u6837\u3002\u53ef\u91cd\u5165\u9501\u53ef\u4ee5\u907f\u514d\u6b7b\u9501\u7684\u95ee\u9898\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class PhonePlus {\n    ReentrantLock lock = new ReentrantLock();\n    public synchronized void sendEmail() {\n        System.out.println(Thread.currentThread().getName() + \"\\t sendEmail...\");\n        sendMessasge();\n    }\n\n    public synchronized void sendMessasge() {\n        System.out.println(Thread.currentThread().getName() + \"\\t sendMessage...\");\n    }\n\n    public void method01() {\n        lock.lock();\n        try {\n            System.out.println(Thread.currentThread().getName() + \"\\t method01...\");\n            method02();\n        } finally {\n            lock.unlock();\n        }\n    }\n\n    public void method02() {\n        lock.lock();\n        try {\n            System.out.println(Thread.currentThread().getName() + \"\\t method02...\");\n        } finally {\n            lock.unlock();\n        }\n    }\n}\n\npublic class ReentrantLockDemo {\n    public static void main(String&#91;] args) {\n        PhonePlus phonePlus = new PhonePlus();\n        new Thread(() -&gt; {\n\/\/            phonePlus.sendEmail();\n            phonePlus.method01();\n        }, \"A\").start();\n\n        new Thread(() -&gt; {\n\/\/            phonePlus.sendEmail();\n            phonePlus.method01();\n        }, \"B\").start();\n    }\n}<\/code><\/pre>\n\n\n\n<p>synchronized\u548cReentrantLock\u90fd\u662f\u53ef\u91cd\u5165\u9501\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u81ea\u65cb\u9501<\/h2>\n\n\n\n<p>\u6240\u8c13\u81ea\u65cb\u9501\uff0c\u5c31\u662f\u5c1d\u8bd5\u83b7\u53d6\u9501\u7684\u7ebf\u7a0b\u4e0d\u4f1a\u7acb\u5373\u963b\u585e\uff0c\u800c\u662f\u91c7\u7528\u5faa\u73af\u7684\u65b9\u5f0f\u53bb\u5c1d\u8bd5\u83b7\u53d6\uff0c\u81ea\u5df1\u5728\u90a3\u513f\u4e00\u76f4\u5faa\u73af\u83b7\u53d6\uff0c\u5c31\u50cf&#8221;\u81ea\u65cb&#8221;\u4e00\u6837\u3002\u8fd9\u6837\u7684\u597d\u5904\u662f\u51cf\u5c11\u7ebf\u7a0b\u5207\u6362\u4e0a\u4e0b\u6587\u7684\u5f00\u9500\uff0c\u7f3a\u70b9\u662f\u4f1a\u6d88\u8017CPU\u3002CAS\u5e95\u5c42\u7684getAndAddInt\u5c31\u662f\u81ea\u65cb\u9501\u601d\u60f3\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class WC {\n    AtomicReference&lt;Thread&gt; atomicReference = new AtomicReference&lt;&gt;();\n\n    public void lock() {\n        Thread currentThread = Thread.currentThread();\n\n        while (!atomicReference.compareAndSet(null, currentThread)) {\n            System.out.println(Thread.currentThread().getName() + \"\\t \u5c1d\u8bd5\u4e2d...\");\n            try {\n                TimeUnit.SECONDS.sleep(1);\n            } catch (InterruptedException e) {\n                throw new RuntimeException(e);\n            }\n        }\n\n        System.out.println(Thread.currentThread().getName() + \"\\t \u83b7\u53d6\u9501...\");\n    }\n\n    public void unlock() {\n        Thread currentThread = Thread.currentThread();\n        atomicReference.compareAndSet(currentThread, null);\n        System.out.println(Thread.currentThread().getName() + \"\\t \u89e3\u9501...\");\n    }\n}\npublic class SpinLockDemo {\n    public static void main(String&#91;] args) {\n        WC wc = new WC();\n\n        new Thread(() -&gt; {\n            wc.lock();\n            try {\n                TimeUnit.SECONDS.sleep(5);\n            } catch (InterruptedException e) {\n                throw new RuntimeException(e);\n            }\n            wc.unlock();\n        }, \"A\").start();\n\n        new Thread(() -&gt; {\n            wc.lock();\n            try {\n                TimeUnit.SECONDS.sleep(2);\n            } catch (InterruptedException e) {\n                throw new RuntimeException(e);\n            }\n            wc.unlock();\n        }, \"B\").start();\n    }\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u8bfb\u5199\u9501\/\u72ec\u5360\u9501\/\u5171\u4eab\u9501<\/h2>\n\n\n\n<p>\u8bfb\u9501\u662f\u5171\u4eab\u7684\uff0c\u5199\u9501\u662f\u72ec\u5360\u7684\u3002juc.ReentrantLock\u548csynchronized\u90fd\u662f\u72ec\u5360\u9501\uff0c\u72ec\u5360\u9501\u5c31\u662f\u4e00\u4e2a\u9501\u667a\u80fd\u88ab\u4e00\u4e2a\u7ebf\u7a0b\u6240\u6301\u6709\u3002\u6709\u7684\u65f6\u5019\uff0c\u9700\u8981\u8bfb\u5199\u5206\u79bb\uff0c\u90a3\u4e48\u5c31\u8981\u5f15\u5165\u8bfb\u5199\u9501\uff0c\u5373juc.ReentrantReadWriteLock\u3002<br>\u72ec\u5360\u9501\uff1a\u6307\u8be5\u9501\u4e00\u6b21\u667a\u80fd\u88ab\u4e00\u4e2a\u7ebf\u7a0b\u6240\u6301\u6709\uff0c\u5bf9ReentrantLock\u548cSynchronized\u800c\u8a00\u90fd\u662f\u72ec\u5360\u9501\u3002<br>\u5171\u4eab\u9501\uff1a\u6307\u8be5\u9501\u53ef\u60b2\u591a\u4e2a\u7ebf\u7a0b\u6240\u6301\u6709\u3002<br>\u5bf9ReentrantReadWriteLock\u5176\u8bfb\u9501\u662f\u5171\u4eab\u9501\uff0c\u5176\u5199\u9501\u662f\u72ec\u5360\u9501\u3002<br>\u8be5\u9501\u7684\u5171\u4eab\u9501\u53ef\u4fdd\u8bc1\u5e76\u53d1\u8bfb\u662f\u975e\u5e38\u9ad8\u6548\u7684\uff0c\u8bfb\u5199\u3001\u5199\u8bfb\u3001\u5199\u5199\u7684\u8fc7\u7a0b\u662f\u4e92\u65a5\u7684\u3002<br>\u6bd4\u5982\u7f13\u5b58\uff0c\u5c31\u9700\u8981\u8bfb\u5199\u9501\u6765\u63a7\u5236\u3002\u7f13\u5b58\u5c31\u662f\u4e00\u4e2a\u952e\u503c\u5bf9\uff0c\u4e00\u4e0bDemo\u6a21\u62df\u4e86\u7f13\u5b58\u7684\u8bfb\u5199\u64cd\u4f5c\uff0c\u8bfb\u7684get\u65b9\u6cd5\u4f7f\u7528\u4e86ReentrantReadWriteLock.ReadLock()\uff0c\u5199\u7684put\u65b9\u6cd5\u4f7f\u7528\u4e86ReentrantReadWriteLock.WriteLock()\u3002\u8fd9\u6837\u907f\u514d\u4e86\u5199\u88ab\u6253\u65ad\uff0c\u5b9e\u73b0\u4e86\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u8bfb\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Cache {\n    private final Map&lt;String, Object&gt; map = new ConcurrentHashMap&lt;&gt;();\n    private final ReentrantReadWriteLock reentrantReadWriteLock = new ReentrantReadWriteLock();\n\n    \/**\n     * \u8bb0\u5f55\u90a3\u4e2a\u7ebf\u7a0b\u6765\u7b7e\u5b57\n     * @param key \u7ebf\u7a0b\u540d\n     * @param value \u7b7e\u5b57\u5185\u5bb9\n     *\/\n    public void put(String key, Object value) {\n        reentrantReadWriteLock.writeLock().lock();\n        try {\n            System.out.println(key + \"\u6b63\u5728\u7b7e\u5b57\uff1a\" + value);\n            \/\/ \u8017\u65f6\u64cd\u4f5c\n            try {\n                TimeUnit.SECONDS.sleep(1);\n            } catch (InterruptedException e) {\n                throw new RuntimeException(e);\n            }\n            map.put(key, value);\n            System.out.println(\"\u7b7e\u5b57\u5b8c\u6210\");\n        } finally {\n            reentrantReadWriteLock.writeLock().unlock();\n        }\n    }\n\n    public Object get(String key) {\n        Object result = null;\n        reentrantReadWriteLock.readLock().lock();\n        try {\n            System.out.println(key + \"\u6b63\u5728\u770b\u7b7e\u5b57\");\n            \/\/ \u8017\u65f6\u64cd\u4f5c\n            try {\n                TimeUnit.SECONDS.sleep(2);\n            } catch (InterruptedException e) {\n                throw new RuntimeException(e);\n            }\n            result = map.get(key);\n            System.out.println(\"\u770b\u5b8c\u4e86\");\n        } finally {\n            reentrantReadWriteLock.readLock().unlock();\n        }\n        return result;\n    }\n}\n\npublic class ReadWriteLockDemo {\n    public static void main(String&#91;] args) {\n        Cache cache = new Cache();\n        for (int i = 1; i &lt;= 5; i++) {\n            final int temp = i;\n            new Thread(() -&gt; {\n                cache.put(\"\u7ebf\u7a0b\u540d\" + temp, \"\u6570\u636e\" + temp);\n            }, String.valueOf(i)).start();\n        }\n\n        for (int i = 1; i &lt;= 5; i++) {\n            final int temp = i;\n            new Thread(() -&gt; {\n                cache.get(\"\u7ebf\u7a0b\u540d\" + temp);\n            }, String.valueOf(i)).start();\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u963b\u585e\u961f\u5217<\/h2>\n\n\n\n<p>\u6982\u5ff5\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5728\u591a\u7ebf\u7a0b\u9886\u57df\uff1a\u6240\u8c13\u963b\u585e\uff0c\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u4f1a\u6302\u8d77\u7ebf\u7a0b\uff08\u5373\u963b\u585e\uff09\uff0c\u4e00\u65e6\u6761\u4ef6\u6ee1\u8db3\uff0c\u88ab\u6302\u8d77\u7684\u7ebf\u7a0b\u53c8\u4f1a\u81ea\u52a8\u88ab\u5524\u9192\u3002<\/li>\n\n\n\n<li>\u963b\u585e\u961f\u5217\u662f\u4e00\u4e2a\u961f\u5217\uff0c\u5728\u6570\u636e\u7ed3\u6784\u4e2d\u8d77\u7684\u4f5c\u7528\u5982\u4e0b\u56fe\uff1a<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"854\" height=\"356\" src=\"http:\/\/www.spiceandwolf.com.cn\/wp-content\/uploads\/2024\/07\/image-69.png\" alt=\"\" class=\"wp-image-1268\" srcset=\"http:\/\/www.spiceandwolf.com.cn\/wp-content\/uploads\/2024\/07\/image-69.png 854w, http:\/\/www.spiceandwolf.com.cn\/wp-content\/uploads\/2024\/07\/image-69-300x125.png 300w, http:\/\/www.spiceandwolf.com.cn\/wp-content\/uploads\/2024\/07\/image-69-768x320.png 768w\" sizes=\"auto, (max-width: 854px) 100vw, 854px\" \/><\/figure>\n\n\n\n<p>\u5f53\u961f\u5217\u662f\u7a7a\u7684\uff0c\u4ece\u961f\u5217\u4e2d\u83b7\u53d6\uff08take\uff09\u5143\u7d20\u7684\u64cd\u4f5c\u5c06\u4f1a\u88ab\u963b\u585e\u3002<br>\u5f53\u961f\u5217\u662f\u6ee1\u7684\uff0c\u5411\u961f\u5217\u4e2d\u6dfb\u52a0\uff08put\uff09\u5143\u7d20\u7684\u64cd\u4f5c\u5c06\u4f1a\u88ab\u963b\u585e\u3002<br>\u8bd5\u56fe\u4ece\u7a7a\u7684\u961f\u5217\u4e2d\u83b7\u53d6\u5143\u7d20\u7684\u7ebf\u7a0b\u5c06\u4f1a\u88ab\u963b\u585e\uff0c\u77e5\u9053\u5176\u4ed6\u7ebf\u7a0b\u5f80\u7a7a\u7684\u961f\u5217\u4e2d\u63d2\u5165\u65b0\u7684\u5143\u7d20\u3002<br>\u8bd5\u56fe\u5411\u6ee1\u7684\u961f\u5217\u4e2d\u6dfb\u52a0\u65b0\u5143\u7d20\u7684\u7ebf\u7a0b\u5c06\u4f1a\u88ab\u963b\u585e\uff0c\u77e5\u9053\u5176\u4ed6\u7ebf\u7a0b\u4e2d\u961f\u5217\u4e2d\u9038\u51fa\u4e00\u4e2a\u6216\u591a\u4e2a\u5143\u7d20\u6d3b\u7740\u5b8c\u5168\u60c5\u51b5\uff0c\u4f7f\u961f\u5217\u7a7a\u95f2\u8d77\u6765\u540e\u5e76\u540e\u7eed\u65b0\u589e\u3002<br>\u597d\u5904\uff1a\u963b\u585e\u961f\u5217\u4e0d\u7528\u624b\u52a8\u63a7\u5236\u4ec0\u4e48\u65f6\u5019\u88ab\u963b\u585e\uff0c\u4ec0\u4e48\u65f6\u5019\u6539\u88ab\u5524\u9192\uff0c\u7b80\u5316\u4e86\u64cd\u4f5c\u3002<br>\u4f53\u7cfb\uff1aCollection->Queue->BlockingQueue->\u4e03\u4e2a\u963b\u585e\u961f\u5217\u5b9e\u73b0\u7c7b<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td>\u7c7b\u540d<\/td><td>\u7279\u70b9<\/td><\/tr><tr><td>ArrayBlockingQueue<\/td><td>\u7531\u6570\u7ec4\u7ed3\u6784\u6784\u6210\u7684\u6709\u754c\u963b\u585e\u961f\u5217<\/td><\/tr><tr><td>LinkedBlockingQueue<\/td><td>\u7531\u94fe\u8868\u7ed3\u6784\u6784\u6210\u7684\u6709\u754c\uff08\u9ed8\u8ba4\u503c\u4e3aInteger.MAX_VALUE\uff09\u963b\u585e\u961f\u5217<\/td><\/tr><tr><td>PriorityBlockingQueue<\/td><td>\u652f\u6301\u4f18\u5148\u7ea7\u6392\u5e8f\u7684\u65e0\u754c\u963b\u585e\u961f\u5217<\/td><\/tr><tr><td>DelayQueue<\/td><td>\u4f7f\u7528\u4f18\u5148\u7ea7\u961f\u5217\u5b9e\u73b0\u7684\u5ef6\u8fdf\u65e0\u754c\u963b\u585e\u961f\u5217<\/td><\/tr><tr><td>SynchronousQueue<\/td><td>\u4e0d\u5b58\u50a8\u5143\u7d20\u7684\u963b\u585e\u961f\u5217\uff0c\u4e5f\u5373\u5355\u4e2a\u5143\u7d20\u7684\u961f\u5217<\/td><\/tr><tr><td>LinkedTransferQueue<\/td><td>\u7531\u94fe\u8868\u6784\u6210\u7684\u65e0\u754c\u963b\u585e\u961f\u5217<\/td><\/tr><tr><td>LinkedBlockingDeque<\/td><td>\u7531\u94fe\u8868\u6784\u6210\u7684\u53cc\u5411\u963b\u585e\u961f\u5217<\/td><\/tr><\/tbody><\/table><figcaption class=\"wp-element-caption\">7\u4e2a\u963b\u585e\u961f\u5217\u53ca\u7279\u70b9<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td>\u65b9\u6cd5\u7c7b\u578b<\/td><td>\u629b\u51fa\u5f02\u5e38<\/td><td>\u8fd4\u56de\u5e03\u5c14<\/td><td>\u963b\u585e<\/td><td>\u8d85\u65f6<\/td><\/tr><tr><td>\u63d2\u5165<\/td><td>add(E e)<\/td><td>offer(E e)<\/td><td>put(E e)<\/td><td>offer(E e, time, timeUnit)<\/td><\/tr><tr><td>\u53d6\u51fa<\/td><td>remove()<\/td><td>poll()<\/td><td>take()<\/td><td>poll(Time, TimeUnit)<\/td><\/tr><tr><td>\u961f\u9996<\/td><td>element()<\/td><td>peek()<\/td><td>\u65e0<\/td><td>\u65e0<\/td><\/tr><\/tbody><\/table><figcaption class=\"wp-element-caption\">\u963b\u585e\u961f\u5217API<\/figcaption><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u629b\u51fa\u5f02\u5e38\u662f\u6307\u5f53\u961f\u5217\u6ee1\u65f6\uff0c\u518d\u6b21\u63d2\u5165\u4f1a\u629b\u51fa\u5f02\u5e38\uff1b<\/li>\n\n\n\n<li>\u8fd4\u56de\u5e03\u5c14\u662f\u6307\u5f53\u961f\u5217\u6ee1\u65f6\uff0c\u518d\u6b21\u63d2\u5165\u4f1a\u8fd4\u56defalse\uff1b\u963b\u585e\u662f\u6307\u5f53\u961f\u5217\u6ee1\u65f6\uff0c\u518d\u6b21\u63d2\u5165\u4f1a\u88ab\u963b\u585e\uff0c\u77e5\u9053\u961f\u5217\u51fa\u4e00\u4e2a\u5143\u7d20\uff0c\u624d\u80fd\u63d2\u5165\u3002\u8d85\u65f6\u662f\u6307\u5f53\u4e00\u4e2a\u65f6\u9650\u8fc7\u540e\uff0c\u624d\u4f1a\u63d2\u5165\u6216\u8005\u53d6\u51fa\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>public class BlockingQuereDemo {\n    public static void main(String&#91;] args) throws InterruptedException {\n        BlockingQueue&lt;String&gt; queue = new ArrayBlockingQueue&lt;&gt;(3);\n\n\/\/        addAndRemove(queue);\n\n\/\/        offerAndPoll(queue);\n\n\/\/        putAndTake(queue);\n\n        outOfTime(queue);\n    }\n\n    private static void outOfTime(BlockingQueue&lt;String&gt; queue) throws InterruptedException {\n        System.out.println(queue.offer(\"AA\", 2, TimeUnit.SECONDS));\n        System.out.println(queue.offer(\"BB\", 2, TimeUnit.SECONDS));\n        System.out.println(queue.offer(\"CC\", 2, TimeUnit.SECONDS));\n        System.out.println(queue.offer(\"DD\", 2, TimeUnit.SECONDS));\n\n        System.out.println(queue.poll(4, TimeUnit.SECONDS));\n        System.out.println(queue.poll(4, TimeUnit.SECONDS));\n        System.out.println(queue.poll(4, TimeUnit.SECONDS));\n        System.out.println(queue.poll(4, TimeUnit.SECONDS));\n    }\n\n    private static void putAndTake(BlockingQueue&lt;String&gt; queue) throws InterruptedException {\n        queue.put(\"aa\");\n        queue.put(\"bb\");\n        queue.put(\"cc\");\n        queue.put(\"dd\");\n\n        System.out.println(queue.take());\n        System.out.println(queue.take());\n        System.out.println(queue.take());\n        System.out.println(queue.take());\n    }\n\n    private static void offerAndPoll(BlockingQueue&lt;String&gt; queue) {\n        System.out.println(queue.offer(\"aa\"));\n        System.out.println(queue.offer(\"bb\"));\n        System.out.println(queue.offer(\"cc\"));\n        System.out.println(queue.offer(\"dd\"));\n\n        System.out.println(queue.peek());\n\n        System.out.println(queue.poll());\n        System.out.println(queue.poll());\n        System.out.println(queue.poll());\n        System.out.println(queue.poll());\n    }\n\n    private static void addAndRemove(BlockingQueue&lt;String&gt; queue) {\n        queue.add(\"a\");\n        queue.add(\"b\");\n        queue.add(\"c\");\n\/\/        queue.add(\"d\");\n\n        System.out.println(queue.element());\n\n        System.out.println(queue.remove());\n        System.out.println(queue.remove());\n        System.out.println(queue.remove());\n        System.out.println(queue.remove());\n    }\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>public class SynchronousQueueDemo {\n    public static void main(String&#91;] args) {\n        BlockingQueue&lt;String&gt; blockingQueue = new SynchronousQueue&lt;&gt;();\n\n        new Thread(() -&gt; {\n            try {\n                blockingQueue.put(\"1\");\n                System.out.println(Thread.currentThread().getName() + \"put 1\");\n                blockingQueue.put(\"2\");\n                System.out.println(Thread.currentThread().getName() + \"put 2\");\n                blockingQueue.put(\"3\");\n                System.out.println(Thread.currentThread().getName() + \"put 3\");\n            } catch (InterruptedException e) {\n                throw new RuntimeException(e);\n            }\n        }, \"\u5e08\u5085\").start();\n\n        new Thread(() -&gt; {\n            try {\n                blockingQueue.take();\n                System.out.println(Thread.currentThread().getName() + \"take 1\");\n                blockingQueue.take();\n                System.out.println(Thread.currentThread().getName() + \"take 2\");\n                blockingQueue.take();\n                System.out.println(Thread.currentThread().getName() + \"take 3\");\n            } catch (InterruptedException e) {\n                throw new RuntimeException(e);\n            }\n        }, \"\u5ba2\u6237\").start();\n    }\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u963b\u585e\u961f\u5217\u7684\u5e94\u7528-\u751f\u4ea7\u8005\u6d88\u8d39\u8005<\/h2>\n\n\n\n<h4 class=\"wp-block-heading\">synchronized\u5b9e\u73b0<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>class MyResource {\n    \/\/ \u7528\u4e8e\u8bb0\u5f55\u5305\u5b50\u6570\u91cf\n    int number = 0;\n\n    public synchronized void increment() throws InterruptedException {\n        while (number != 0) {\n            this.wait();\n        }\n\n        number++;\n        System.out.println(Thread.currentThread().getName() + \"\\t\" + number);\n\n        this.notifyAll();\n    }\n\n    public synchronized void decrement() throws InterruptedException {\n        while (number == 0) {\n            this.wait();\n        }\n\n        number--;\n        System.out.println(Thread.currentThread().getName() + \"\\t\" + number);\n\n        this.notifyAll();\n    }\n}\n\npublic class ProdConsDemo {\n    public static void main(String&#91;] args) {\n        MyResource myResource = new MyResource();\n\n        new Thread(() -&gt; {\n            for (int i = 0; i &lt; 10; i++) {\n                try {\n                    myResource.increment();\n                } catch (InterruptedException e) {\n                    throw new RuntimeException(e);\n                }\n            }\n        }, \"\u5e08\u50851\").start();\n\n        new Thread(() -&gt; {\n            for (int i = 0; i &lt; 10; i++) {\n                try {\n                    myResource.increment();\n                } catch (InterruptedException e) {\n                    throw new RuntimeException(e);\n                }\n            }\n        }, \"\u5e08\u50852\").start();\n\n        new Thread(() -&gt; {\n            for (int i = 0; i &lt; 10; i++) {\n                try {\n                    myResource.decrement();\n                } catch (InterruptedException e) {\n                    throw new RuntimeException(e);\n                }\n            }\n        }, \"\u987e\u5ba21\").start();\n\n        new Thread(() -&gt; {\n            for (int i = 0; i &lt; 10; i++) {\n                try {\n                    myResource.decrement();\n                } catch (InterruptedException e) {\n                    throw new RuntimeException(e);\n                }\n            }\n        }, \"\u987e\u5ba22\").start();\n    }\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">lock\u5b9e\u73b0\uff08\u7cbe\u51c6\u901a\u77e5\u987a\u5e8f\u8bbf\u95ee\uff09<\/h2>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"674\" height=\"500\" src=\"http:\/\/www.spiceandwolf.com.cn\/wp-content\/uploads\/2024\/07\/image-73.png\" alt=\"\" class=\"wp-image-1272\" style=\"width:233px;height:auto\" srcset=\"http:\/\/www.spiceandwolf.com.cn\/wp-content\/uploads\/2024\/07\/image-73.png 674w, http:\/\/www.spiceandwolf.com.cn\/wp-content\/uploads\/2024\/07\/image-73-300x223.png 300w\" sizes=\"auto, (max-width: 674px) 100vw, 674px\" \/><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>class ShareData {\n    Lock lock = new ReentrantLock();\n    Condition c1 = lock.newCondition();\n    Condition c2 = lock.newCondition();\n    Condition c3 = lock.newCondition();\n\n    int state = 1;\n\n    public void printC1() {\n        lock.lock();\n        try {\n            while (state != 1) {\n                System.out.println(\"c1\u5c1d\u8bd5\u83b7\u53d6\u9501\u5931\u8d25\");\n                c1.await();\n            }\n            state = 2;\n            for (int i = 0; i &lt; 1; i++) {\n                System.out.println(Thread.currentThread().getName() + \"\\t\" + i);\n            }\n            c2.signalAll();\n        } catch (InterruptedException e) {\n            throw new RuntimeException(e);\n        } finally {\n            lock.unlock();\n        }\n    }\n\n    public void printC2() {\n        lock.lock();\n        try {\n            while (state != 2) {\n                System.out.println(\"c2\u5c1d\u8bd5\u83b7\u53d6\u9501\u5931\u8d25\");\n                c2.await();\n            }\n            state = 3;\n            for (int i = 0; i &lt; 2; i++) {\n                System.out.println(Thread.currentThread().getName() + \"\\t\" + i);\n            }\n            c3.signalAll();\n        } catch (InterruptedException e) {\n            throw new RuntimeException(e);\n        } finally {\n            lock.unlock();\n        }\n    }\n\n    public void printC3() {\n        lock.lock();\n        try {\n            while (state != 3) {\n                System.out.println(\"c3\u5c1d\u8bd5\u83b7\u53d6\u9501\u5931\u8d25\");\n                c3.await();\n            }\n            state = 1;\n            for (int i = 0; i &lt; 3; i++) {\n                System.out.println(Thread.currentThread().getName() + \"\\t\" + i);\n            }\n            c1.signalAll();\n        } catch (InterruptedException e) {\n            throw new RuntimeException(e);\n        } finally {\n            lock.unlock();\n        }\n    }\n}\npublic class ConditionDemo {\n    public static void main(String&#91;] args) {\n        ShareData shareData = new ShareData();\n\n        new Thread(() -&gt; {\n            for (int i = 0; i &lt; 10; i++) {\n                shareData.printC1();\n            }\n        }, \"A\").start();\n\n        new Thread(() -&gt; {\n            for (int i = 0; i &lt; 10; i++) {\n                shareData.printC2();\n            }\n        }, \"B\").start();\n\n        new Thread(() -&gt; {\n            for (int i = 0; i &lt; 10; i++) {\n                shareData.printC3();\n            }\n        }, \"C\").start();\n    }\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Synchrinized\u548cLock\u533a\u522b<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u539f\u59cb\u6784\u6210\u3002sync\u662fJVM\u5c42\u9762\u7684\uff0c\u5e95\u5c42\u901a\u8fc7monitorenter\u548cmonitorexit\u6765\u5b9e\u73b0\u7684\u3002Lock\u662fJDK API\u5c42\u9762\u7684\u3002<\/li>\n\n\n\n<li>\u4f7f\u7528\u65b9\u6cd5\u3002sync\u4e0d\u9700\u8981\u624b\u52a8\u91ca\u653e\u9501\uff0c\u800cLock\u9700\u8981\u624b\u52a8\u91ca\u653e\u3002<\/li>\n\n\n\n<li>\u662f\u5426\u53ef\u7ec8\u7aef\u3002sync\u4e0d\u53ef\u4e2d\u65ad\uff0c\u9664\u975e\u629b\u51fa\u5f02\u5e38\u6216\u8005\u6b63\u5e38\u8fd0\u884c\u5b8c\u6210\u3002Lock\u662f\u53ef\u4e2d\u65ad\u7684\uff0c\u901a\u8fc7\u8c03\u7528interrupt()\u65b9\u6cd5\u3002<\/li>\n\n\n\n<li>\u662f\u5426\u4e3a\u516c\u5e73\u9501\u3002sync\u53ea\u80fd\u662f\u975e\u516c\u5e73\u9501\uff0c\u800cLock\u65e2\u80fd\u662f\u516c\u5e73\u9501\uff0c\u53c8\u80fd\u662f\u975e\u516c\u5e73\u9501\u3002<\/li>\n\n\n\n<li>\u7ed1\u5b9a\u591a\u4e2a\u6761\u4ef6\u3002sync\u4e0d\u80fd\uff0c\u53ea\u80fd\u968f\u673a\u5524\u9192\u3002\u800cLock\u53ef\u4ee5\u901a\u8fc7Condition\u6765\u7ed1\u5b9a\u591a\u4e2a\u6761\u4ef6\uff0c\u7cbe\u786e\u5524\u9192\u3002<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">\u963b\u585e\u961f\u5217\u6a21\u5f0f\u751f\u4ea7\u8005\u6d88\u8d39\u8005<\/h2>\n\n\n\n<p>\u4e3a\u4ec0\u4e48\u9700\u8981BlockingQueue\uff1f<br>\u597d\u5904\u662f\u6211\u4eec\u4e0d\u9700\u8981\u5173\u5fc3\u4ec0\u4e48\u65f6\u5019\u9700\u8981\u963b\u585e\u7ebf\u7a0b\uff0c\u4ec0\u4e48\u65f6\u5019\u9700\u8981\u5524\u9192\u7ebf\u7a0b\uff0c\u56e0\u4e3a\u8fd9\u4e00\u5207BlockingQueue\u90fd\u7ed9\u4f60\u4e00\u624b\u5305\u529e\u597d\u4e86\uff0c\u4f7f\u7528\u963b\u585e\u961f\u5217\u540e\u5c31\u4e0d\u9700\u8981\u624b\u52a8\u52a0\u9501\u4e86\u3002<br>\u5728Concurrent\u5305\u53d1\u5e03\u4e4b\u524d\uff0c\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e0b\uff0c\u6211\u4eec\u6bcf\u4e2a\u57ce\u9700\u8981\u90fd\u5fc5\u987b\u53bb\u81ea\u5df1\u63a7\u5236\u8fd9\u4e9b\u7ec6\u8282\uff0c\u5c24\u5176\u8fd8\u8981\u517c\u987e\u6548\u7387\u548c\u7ebf\u7a0b\u5b89\u5168\uff0c\u800c\u8fd9\u4f1a\u7ed9\u6211\u4eec\u7684\u7a0b\u5e8f\u5e26\u6765\u4e0d\u5c0f\u7684\u590d\u6742\u7684\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class MyRes {\n    private volatile boolean flag = true;\n    private AtomicInteger atomicInteger = new AtomicInteger();\n\n    BlockingQueue&lt;String&gt; blockingQueue = new ArrayBlockingQueue&lt;&gt;(3);\n\n    public void produce() throws InterruptedException {\n        while (flag) {\n            String data = atomicInteger.incrementAndGet() + \"\";\n            boolean result = blockingQueue.offer(data, 2, TimeUnit.SECONDS);\n            if (result) {\n                System.out.println(Thread.currentThread().getName() + \"\\t \u63d2\u5165\u961f\u5217\" + data + \"...\u751f\u4ea7\u6210\u529f\");\n            } else {\n                System.out.println(Thread.currentThread().getName() + \"\\t \u63d2\u5165\u961f\u5217\" + data + \"...\u751f\u4ea7\u5931\u8d25\");\n            }\n            TimeUnit.SECONDS.sleep(1);\n        }\n        System.out.println(Thread.currentThread().getName() + \"\\t \u505c\u5de5\u751f\u4ea7\");\n    }\n\n    public void consume() throws InterruptedException {\n        while (flag) {\n            String result = blockingQueue.poll(2, TimeUnit.SECONDS);\n            if (result == null || \"\".equals(result)) {\n                System.out.println(Thread.currentThread().getName() + \"\\t \u6ca1\u6709\u4e70\u5230...\");\n                return;\n            }\n            System.out.println(Thread.currentThread().getName() + \"\\t \u8d2d\u4e70\u6210\u529f...\");\n        }\n    }\n\n    public void stop() {\n        try {\n            TimeUnit.SECONDS.sleep(5);\n        } catch (InterruptedException e) {\n            throw new RuntimeException(e);\n        }\n        System.out.println(\"\u505c\u5de5\");\n        flag = false;\n    }\n}\n\npublic class ProdConsBlockingQueueDemo {\n\n    public static void main(String&#91;] args) {\n        MyRes myRes = new MyRes();\n\n        new Thread(() -&gt; {\n            System.out.println(Thread.currentThread().getName() + \"\\t \u751f\u4ea7\u7ebf\u7a0b\u542f\u52a8...\");\n            try {\n                myRes.produce();\n            } catch (InterruptedException e) {\n                throw new RuntimeException(e);\n            }\n        }, \"\u5e08\u5085A\").start();\n\n        new Thread(() -&gt; {\n            System.out.println(Thread.currentThread().getName() + \"\\t \u751f\u4ea7\u7ebf\u7a0b\u542f\u52a8...\");\n            try {\n                myRes.produce();\n            } catch (InterruptedException e) {\n                throw new RuntimeException(e);\n            }\n        }, \"\u5e08\u5085B\").start();\n\n        new Thread(() -&gt; {\n            System.out.println(Thread.currentThread().getName() + \"\\t \u6d88\u8d39\u7ebf\u7a0b\u542f\u52a8...\");\n            try {\n                myRes.consume();\n            } catch (InterruptedException e) {\n                throw new RuntimeException(e);\n            }\n        }, \"\u987e\u5ba2A\").start();\n\n        new Thread(() -&gt; {\n            System.out.println(Thread.currentThread().getName() + \"\\t \u6d88\u8d39\u7ebf\u7a0b\u542f\u52a8...\");\n            try {\n                myRes.consume();\n            } catch (InterruptedException e) {\n                throw new RuntimeException(e);\n            }\n        }, \"\u987e\u5ba2B\").start();\n\n        new Thread(() -&gt; {\n            System.out.println(Thread.currentThread().getName() + \"\\t \u6d88\u8d39\u7ebf\u7a0b\u542f\u52a8...\");\n            try {\n                myRes.consume();\n            } catch (InterruptedException e) {\n                throw new RuntimeException(e);\n            }\n        }, \"\u987e\u5ba2C\").start();\n\n        new Thread(() -&gt; {\n            System.out.println(Thread.currentThread().getName() + \"\\t \u6d88\u8d39\u7ebf\u7a0b\u542f\u52a8...\");\n            try {\n                myRes.consume();\n            } catch (InterruptedException e) {\n                throw new RuntimeException(e);\n            }\n        }, \"\u987e\u5ba2D\").start();\n\n        myRes.stop();\n    }\n}<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>JUC\u5e76\u53d1\u5305\u4ecb\u7ecd JMM(Java Memory Model\uff0cjava\u5185\u5b58\u6a21\u578b) JMM\u662f\u6307java\u5185\u5b58\u6a21\u578b\uff0c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25,85,87],"tags":[],"class_list":["post-1248","post","type-post","status-publish","format-standard","hentry","category-code","category-interview","category-java"],"_links":{"self":[{"href":"http:\/\/www.spiceandwolf.com.cn\/index.php\/wp-json\/wp\/v2\/posts\/1248","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.spiceandwolf.com.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.spiceandwolf.com.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.spiceandwolf.com.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.spiceandwolf.com.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1248"}],"version-history":[{"count":8,"href":"http:\/\/www.spiceandwolf.com.cn\/index.php\/wp-json\/wp\/v2\/posts\/1248\/revisions"}],"predecessor-version":[{"id":1412,"href":"http:\/\/www.spiceandwolf.com.cn\/index.php\/wp-json\/wp\/v2\/posts\/1248\/revisions\/1412"}],"wp:attachment":[{"href":"http:\/\/www.spiceandwolf.com.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1248"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.spiceandwolf.com.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1248"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.spiceandwolf.com.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1248"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}