{"id":17,"date":"2022-11-05T23:09:53","date_gmt":"2022-11-05T15:09:53","guid":{"rendered":"http:\/\/codermr.com\/?p=17"},"modified":"2023-03-25T23:34:32","modified_gmt":"2023-03-25T15:34:32","slug":"talk_jmm","status":"publish","type":"post","link":"http:\/\/codermr.com\/index.php\/2022\/11\/05\/talk_jmm\/","title":{"rendered":"Java \u5185\u5b58\u6a21\u578b"},"content":{"rendered":"\n<p>\u6211\u4eec\u77e5\u9053\u865a\u62df\u673a\uff08Virtual Machine\uff09\u662f\u6307\u901a\u8fc7\u8f6f\u4ef6\u6a21\u62df\u7684\u5177\u6709\u5b8c\u6574\u786c\u4ef6\u7cfb\u7edf\u529f\u80fd\u7684\u3001\u8fd0\u884c\u5728\u4e00\u4e2a\u5b8c\u5168\u9694\u79bb\u73af\u5883\u4e2d\u7684\u5b8c\u6574\u8ba1\u7b97\u673a\u7cfb\u7edf\uff0c\u6240\u4ee5 Java \u865a\u62df\u673a\u548c\u771f\u6b63\u7684\u8ba1\u7b97\u673a\u4e00\u6837\uff0c\u4e5f\u6709\u5b83\u81ea\u5df1\u7684\u5185\u5b58\u6a21\u578b &#8211; \u79f0\u4e3a Java \u5185\u5b58\u6a21\u578b\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u8981\u5f00\u53d1\u8bbe\u8ba1\u6b63\u786e\u7684\u5e76\u53d1\u7a0b\u5e8f\uff0c\u4e86\u89e3 Java \u5185\u5b58\u6a21\u578b\u975e\u5e38\u91cd\u8981\u3002<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\"><strong>Java \u5185\u5b58\u6a21\u578b\u89c4\u5b9a\u4e86\u4e0d\u540c\u7684\u7ebf\u7a0b\u5982\u4f55\u3001\u4ee5\u53ca\u4f55\u65f6\u53ef\u4ee5\u770b\u5230\u5176\u4ed6\u7ebf\u7a0b\u5199\u5165\u5171\u4eab\u53d8\u91cf\u7684\u503c\uff0c\u4ee5\u53ca\u5982\u4f55\u5728\u5fc5\u8981\u65f6\u540c\u6b65\u5bf9\u5171\u4eab\u53d8\u91cf\u7684\u8bbf\u95ee<\/strong>\u3002<\/mark>Java 1.5 \u5bf9 Java \u5185\u5b58\u6a21\u578b\u8fdb\u884c\u4e86\u4fee\u6539\u548c\u4f18\u5316\uff0c\u8fd9\u4e2a\u7248\u672c\u7684 Java \u5185\u5b58\u6a21\u578b\u4eca\u5929\u4ecd\u5728\u4f7f\u7528\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Java \u5185\u5b58\u6a21\u578b<\/h2>\n\n\n\n<p>JVM \u5185\u90e8\u4f7f\u7528\u7684 Java \u5185\u5b58\u6a21\u578b\uff0c\u5728<strong>\u7ebf\u7a0b\u5806\u6808<\/strong>\u548c<strong>\u5806<\/strong>\u4e4b\u95f4\u5212\u5206\u5185\u5b58\u3002\u6b64\u56fe\u4ece\u903b\u8f91\u89d2\u5ea6\u8bf4\u660e\u4e86 Java \u5185\u5b58\u6a21\u578b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/jenkov.com\/images\/java-concurrency\/java-memory-model-1.png\" alt=\"\u4ece\u903b\u8f91\u89d2\u5ea6\u770b Java \u5185\u5b58\u6a21\u578b\" width=\"269\" height=\"208\"\/><\/figure>\n\n\n\n<p>\u6211\u4eec\u77e5\u9053\uff0cJava \u865a\u62df\u673a\u4e2d\u8fd0\u884c\u7684\u6bcf\u4e2a\u7ebf\u7a0b\u90fd\u6709\u81ea\u5df1\u7684\u7ebf\u7a0b\u5806\u6808\u3002\u7ebf\u7a0b\u5806\u6808\u5305\u542b\u4e86\u6bcf\u4e2a\u65b9\u6cd5\u4e2d\u7684\u5c40\u90e8\u53d8\u91cf\u3001\u8c03\u7528\u4e86\u54ea\u4e9b\u65b9\u6cd5\u7b49\u4fe1\u606f\u3002\u4e0d\u540c\u7ebf\u7a0b\u5806\u6808\u4e0a\u7684\u4fe1\u606f\uff0c\u5f7c\u6b64\u4e4b\u95f4\u662f\u4e0d\u53ef\u89c1\u3001\u4e0d\u53ef\u4e92\u76f8\u8bbf\u95ee\u7684\u3002\u540c\u4e00\u4e2a\u65b9\u6cd5\uff0c\u88ab\u591a\u4e2a\u7ebf\u7a0b\u8c03\u7528\uff0c\u5219\u6bcf\u4e2a\u7ebf\u7a0b\u90fd\u6709\u81ea\u5df1\u7684\u5806\u6808\uff0c\u4e14\u5806\u6808\u4e0a\u7684\u4fe1\u606f\u4e92\u76f8\u9694\u79bb\u3002<\/p>\n\n\n\n<p>\u800c\u7ebf\u7a0b\u521b\u5efa\u7684\u5bf9\u8c61\uff0c\u662f\u5728\u5806\u5185\u5b58\u4e0a\u5b58\u50a8\u7684\u3002\u8fd9\u4e2a\u5927\u5bb6\u5e94\u8be5\u90fd\u77e5\u9053\u3002<\/p>\n\n\n\n<p>\u5806\u6808\u4e2d\u7684\u5c40\u90e8\u53d8\u91cf\u5982\u679c\u662f Java \u4e2d\u7684\u57fa\u672c\u7c7b\u578b\uff08boolean, byte, short, char, int, long, float, double\uff09\uff0c\u662f\u76f4\u63a5\u4fdd\u5b58\u5728\u7ebf\u7a0b\u5806\u6808\u4e0a\u7684\uff1b\u5982\u679c\u662f\u5bf9\u8c61\uff0c\u5219\u5b9e\u9645\u5b58\u50a8\u662f\u5728\u5806\u5185\u5b58\u4e2d\uff0c\u4f46\u7ebf\u7a0b\u7684\u5806\u6808\u4e0a\u4f1a\u4fdd\u5b58\u6307\u5411\u8be5\u5bf9\u8c61\u7684\u5f15\u7528\u3002<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">\u5982\u679c\u4e24\u4e2a\u7ebf\u7a0b\u540c\u65f6\u8c03\u7528\u540c\u4e00\u4e2a\u5bf9\u8c61\u7684\u65b9\u6cd5\uff0c\u5b83\u4eec\u90fd\u53ef\u4ee5\u8bbf\u95ee\u5bf9\u8c61\u7684\u6210\u5458\u53d8\u91cf\uff0c\u4f46\u6bcf\u4e2a\u7ebf\u7a0b\u90fd\u6709\u81ea\u5df1\u7684\u5c40\u90e8\u53d8\u91cf\u526f\u672c\u3010\u91cd\u70b9\u3011<\/mark>\u3002<\/p>\n\n\n\n<p>\u53e6\u5916\u6ce8\u610f\uff0c\u9759\u6001\u7c7b\u53d8\u91cf\u4e5f\u4e0e\u7c7b\u5b9a\u4e49\u4e00\u8d77\u5b58\u50a8\u5728\u5806\u4e0a\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u786c\u4ef6\u5185\u5b58\u67b6\u6784<\/h2>\n\n\n\n<p><strong>\u73b0\u4ee3\u300e<strong>\u786c\u4ef6\u5185\u5b58\u67b6\u6784<\/strong>\u300f\u4e0e \u300e<strong>JVM \u7684\u5185\u5b58\u6a21\u578b<\/strong>\u300f\u662f\u6709\u4e9b\u4e0d\u540c\u7684\u3002<\/strong>\u4e86\u89e3\u786c\u4ef6\u5185\u5b58\u67b6\u6784\u4e5f\u5f88\u91cd\u8981\uff0c\u53ef\u4ee5\u5e2e\u52a9\u4e86\u89e3 Java \u5185\u5b58\u6a21\u578b\u5982\u4f55\u4e0e\u5b83\u4e00\u8d77\u5de5\u4f5c\u3002\u672c\u6587\u4ecb\u7ecd\u5e38\u89c1\u7684\u786c\u4ef6\u5185\u5b58\u67b6\u6784\uff0c\u540e\u9762\u7684\u90e8\u5206\u5c06\u4ecb\u7ecd Java \u5185\u5b58\u6a21\u578b\u5982\u4f55\u4e0e\u5b83\u4e00\u8d77\u5de5\u4f5c\u3002<\/p>\n\n\n\n<p>\u8fd9\u662f\u73b0\u4ee3\u8ba1\u7b97\u673a\u786c\u4ef6\u67b6\u6784\u7684\u7b80\u5316\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/jenkov.com\/images\/java-concurrency\/java-memory-model-4.png\" alt=\"\u73b0\u4ee3\u786c\u4ef6\u5185\u5b58\u67b6\u6784\u3002\" width=\"339\" height=\"299\"\/><\/figure>\n\n\n\n<p>\u73b0\u4ee3\u8ba1\u7b97\u673a\u4e2d\u901a\u5e38\u6709 2 \u4e2a\u6216\u66f4\u591a CPU\uff0c\u4e00\u4e9b CPU \u4e5f\u53ef\u80fd\u5177\u6709\u591a\u4e2a\u5185\u6838\u3002\u5173\u952e\u662f\uff0c\u5728\u5177\u6709 2 \u4e2a\u6216\u66f4\u591a CPU \u7684\u73b0\u4ee3\u8ba1\u7b97\u673a\u4e0a\uff0c\u53ef\u4ee5\u540c\u65f6\u8fd0\u884c\u591a\u4e2a\u7ebf\u7a0b\u3002\u6bcf\u4e2a CPU \u90fd\u80fd\u591f\u5728\u4efb\u4f55\u7ed9\u5b9a\u65f6\u95f4\u8fd0\u884c\u4e00\u4e2a\u7ebf\u7a0b\u3002\u8fd9\u610f\u5473\u7740\u5982\u679c\u60a8\u7684 Java \u5e94\u7528\u7a0b\u5e8f\u662f\u591a\u7ebf\u7a0b\u7684\uff0c\u90a3\u4e48\u6bcf\u4e2a CPU \u4e00\u4e2a\u7ebf\u7a0b\u53ef\u80fd\u4f1a\u5728\u60a8\u7684 Java \u5e94\u7528\u7a0b\u5e8f\u4e2d\u540c\u65f6\uff08\u5e76\u53d1\uff09\u8fd0\u884c\u3002<\/p>\n\n\n\n<p>\u6bcf\u4e2a CPU \u90fd\u5305\u542b\u4e00\u7ec4\u5bc4\u5b58\u5668\uff0c<strong>\u8fd9\u4e9b\u5bc4\u5b58\u5668\u672c\u8d28\u4e0a\u662f CPU \u5185\u5b58<\/strong>\u3002CPU \u5728\u8fd9\u4e9b\u5bc4\u5b58\u5668\u4e0a\u6267\u884c\u64cd\u4f5c\u7684\u901f\u5ea6\u6bd4\u5b83\u5728\u4e3b\u5b58\u50a8\u5668\u4e2d\u5bf9\u53d8\u91cf\u6267\u884c\u7684\u901f\u5ea6\u8981\u5feb\u5f97\u591a\u3002\u8fd9\u662f\u56e0\u4e3a CPU \u8bbf\u95ee\u8fd9\u4e9b\u5bc4\u5b58\u5668\u7684\u901f\u5ea6\u6bd4\u8bbf\u95ee\u4e3b\u5b58\u50a8\u5668\u7684\u901f\u5ea6\u8981\u5feb\u5f97\u591a\u3002<em>\uff08\u53ef\u4ee5\u9605\u8bfb\u738b\u723d\u7684\u300a\u6c47\u7f16\u8bed\u8a00\u300b\u8fd9\u672c\u4e66\u4e86\u89e3\u5bc4\u5b58\u5668\u7684\u4f5c\u7528\uff09<\/em><\/p>\n\n\n\n<p>\u5927\u591a\u6570\u73b0\u4ee3 CPU \u90fd\u6709\u4e00\u5b9a\u5927\u5c0f\u7684\u9ad8\u901f\u7f13\u5b58\u3002<strong>CPU \u53ef\u4ee5\u6bd4\u4e3b\u5b58\u50a8\u5668\u66f4\u5feb\u5730\u8bbf\u95ee\u5176\u9ad8\u901f\u7f13\u5b58\uff0c\u4f46\u901a\u5e38\u4e0d\u5982\u8bbf\u95ee\u5176\u5185\u90e8\u5bc4\u5b58\u5668\u90a3\u4e48\u5feb\u3002<\/strong>\u56e0\u6b64\uff0cCPU \u9ad8\u901f\u7f13\u5b58\u7684\u901f\u5ea6\u4ecb\u4e8e\u5185\u90e8\u5bc4\u5b58\u5668\u548c\u4e3b\u5b58\u50a8\u5668\u7684\u901f\u5ea6\u4e4b\u95f4\uff0c\u4e00\u4e9b CPU \u53ef\u80fd\u6709\u591a\u4e2a\u9ad8\u901f\u7f13\u5b58\uff08L1 \u3001L2\u3001\u3001L3\uff09\u3002<\/p>\n\n\n\n<p>\u8ba1\u7b97\u673a\u7684\u4e3b\u5b58\u50a8\u533a (RAM)\uff0c\u5176\u5b9e\u5c31\u662f\u6211\u4eec\u7ecf\u5e38\u770b\u5230\u7684\u7535\u8111\u914d\u7f6e\u5185\u5b58\u662f4G\u30018G\u300116G \u7684\u90a3\u4e2a\u5185\u5b58\u3002\u6240\u6709 CPU \u90fd\u53ef\u4ee5\u8bbf\u95ee\u4e3b\u5b58\u3002\u4e3b\u5b58\u50a8\u5668\u7684\u5bb9\u91cf\u6bd4 CPU \u7684\u9ad8\u901f\u7f13\u5b58\u5927\u5f97\u591a\u7684\u591a\u3002<\/p>\n\n\n\n<p class=\"has-vivid-cyan-blue-color has-text-color\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">\u901a\u5e38\uff0c\u5f53 CPU \u9700\u8981\u8bbf\u95ee\u4e3b\u5b58\u65f6\uff0c\u5b83\u4f1a\u5c06\u90e8\u5206\u4e3b\u5b58\u4e2d\u7684\u6570\u636e\u8bfb\u5165\u5176 CPU \u9ad8\u901f\u7f13\u5b58\u3002\u5b83\u751a\u81f3\u53ef\u4ee5\u5c06\u90e8\u5206\u9ad8\u901f\u7f13\u5b58\u4e2d\u7684\u6570\u636e\u8bfb\u5165\u5176\u5185\u90e8\u5bc4\u5b58\u5668\uff0c\u7136\u540e\u5bf9\u5176\u6267\u884c\u64cd\u4f5c\u3002\u5f53 CPU \u9700\u8981\u5c06\u7ed3\u679c\u5199\u56de\u4e3b\u5b58\u65f6\uff0c\u5b83\u4f1a\u5c06\u503c\u4ece\u5176\u5185\u90e8\u5bc4\u5b58\u5668\u5237\u65b0\u5230\u9ad8\u901f\u7f13\u5b58\uff0c\u5e76\u5728\u67d0\u4e2a\u65f6\u5019\u5c06\u503c\u5237\u65b0\u56de\u4e3b\u5b58\u3002<\/mark><\/p>\n\n\n\n<p>\u5f53 CPU \u9700\u8981\u5728\u9ad8\u901f\u7f13\u5b58\u4e2d\u5b58\u50a8\u5185\u5bb9\u3001\u4f46\u9ad8\u901f\u7f13\u5b58\u5269\u4f59\u5bb9\u91cf\u4e0d\u8db3\u65f6\uff0c\u5c31\u4f1a\u5c06\u5b58\u50a8\u5728\u9ad8\u901f\u7f13\u5b58\u4e2d\u7684\u503c\u5237\u65b0\u56de\u4e3b\u5185\u5b58\u3002CPU \u9ad8\u901f\u7f13\u5b58\u53ef\u4ee5\u6bcf\u6b21\u5c06\u90e8\u5206\u6570\u636e\u5237\u65b0\u56de\u4e3b\u5185\u5b58\uff0c\u800c\u4e0d\u5fc5\u5728\u6bcf\u6b21\u66f4\u65b0\u6570\u636e\u65f6\u5c06\u5168\u90e8\u6570\u636e\u90fd\u5237\u65b0\u56de\u4e3b\u5b58\u50a8\u5668\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u300eJava \u5185\u5b58\u6a21\u578b\u300f\u548c\u300e\u786c\u4ef6\u5185\u5b58\u67b6\u6784\u300f\u4e4b\u95f4\u7684\u5dee\u8ddd<\/h2>\n\n\n\n<p>\u5982\u524d\u6240\u8ff0\uff0c\u300eJava \u5185\u5b58\u6a21\u578b\u300f\u548c\u300e\u786c\u4ef6\u5185\u5b58\u67b6\u6784\u300f\u662f\u4e0d\u540c\u7684\u3002<strong>\u300e\u786c\u4ef6\u5185\u5b58\u67b6\u6784\u300f\u4e0d\u533a\u5206\u7ebf\u7a0b\u5806\u6808\u548c\u5806\u3002<\/strong>\u5728\u786c\u4ef6\u4e0a\uff0c\u7ebf\u7a0b\u6808\u548c\u5806\u90fd\u4f4d\u4e8e\u4e3b\u5b58\u4e2d\u3002\u90e8\u5206\u7ebf\u7a0b\u5806\u6808\u548c\u5806\u6709\u65f6\u53ef\u80fd\u5b58\u5728\u4e8e CPU \u7f13\u5b58\u548c\u5185\u90e8 CPU \u5bc4\u5b58\u5668\u4e2d\u3002\u5982\u4e0b\u56fe\u6240\u793a\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/jenkov.com\/images\/java-concurrency\/java-memory-model-5.png\" alt=\"CPU\u5185\u90e8\u5bc4\u5b58\u5668\u3001CPU\u7f13\u5b58\u548c\u4e3b\u5b58\u4e4b\u95f4\u7684\u7ebf\u7a0b\u6808\u548c\u5806\u7684\u5212\u5206\u3002\" width=\"613\" height=\"288\"\/><figcaption class=\"wp-element-caption\">\u5728\u786c\u4ef6\u5c42\u9762\u7684\u5185\u5b58\u67b6\u6784\uff0c\u5e76\u4e0d\u533a\u5206\u7ebf\u7a0b\u5806\u6808\u548c\u5806\u3002<\/figcaption><\/figure>\n\n\n\n<p>\u5f53\u5bf9\u8c61\u548c\u53d8\u91cf\u53ef\u4ee5\u5b58\u50a8\u5728\u8ba1\u7b97\u673a\u7684\u5404\u79cd\u4e0d\u540c\u5185\u5b58\u533a\u57df\u4e2d\u65f6\uff0c\u53ef\u80fd\u4f1a\u51fa\u73b0\u67d0\u4e9b\u95ee\u9898\u3002\u4e24\u4e2a\u4e3b\u8981\u95ee\u9898\u662f\uff1a<\/p>\n\n\n\n<ul>\n<li>\u7ebf\u7a0b\u66f4\u65b0\uff08\u5199\u5165\uff09\u5171\u4eab\u53d8\u91cf\u65f6\u7684\u53ef\u89c1\u6027\u95ee\u9898\u3002<\/li>\n\n\n\n<li>\u8bfb\u53d6\u3001\u68c0\u67e5\u548c\u5199\u5165\u5171\u4eab\u53d8\u91cf\u65f6\u7684\u7ade\u4e89\u6761\u4ef6\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u5171\u4eab\u5bf9\u8c61\u7684\u53ef\u89c1\u6027<\/h3>\n\n\n\n<p>\u5982\u679c\u4e24\u4e2a\u6216\u591a\u4e2a\u7ebf\u7a0b\u5171\u4eab\u4e00\u4e2a\u5bf9\u8c61\uff0c\u800c\u6ca1\u6709\u6b63\u786e\u4f7f\u7528 <code>volatile<\/code> \u58f0\u660e\u6216\u540c\u6b65\uff0c\u5219\u4e00\u4e2a\u7ebf\u7a0b\u5bf9\u5171\u4eab\u5bf9\u8c61\u7684\u66f4\u65b0\u53ef\u80fd\u5bf9\u5176\u4ed6\u7ebf\u7a0b\u4e0d\u53ef\u89c1\u3002<\/p>\n\n\n\n<p>\u60f3\u8c61\u4e00\u4e0b\uff0c\u67d0\u5171\u4eab\u5bf9\u8c61\u6700\u521d\u5b58\u50a8\u5728\u4e3b\u5185\u5b58\u4e2d\u3002\u7136\u540e\u5728 CPU \u4e0a\u8fd0\u884c\u7684\u7ebf\u7a0b\u5c06\u5171\u4eab\u5bf9\u8c61\u8bfb\u5165\u5176 CPU \u9ad8\u901f\u7f13\u5b58\u3002\u5b83\u5728\u90a3\u91cc\u5bf9\u5171\u4eab\u5bf9\u8c61\u8fdb\u884c\u4e86\u66f4\u6539\u3002\u53ea\u8981 CPU \u9ad8\u901f\u7f13\u5b58\u6ca1\u6709\u88ab\u5237\u65b0\u56de\u4e3b\u5185\u5b58\uff0c\u5171\u4eab\u5bf9\u8c61\u7684\u66f4\u6539\u7248\u672c\u5bf9\u8fd0\u884c\u5728\u5176\u4ed6 CPU \u4e0a\u7684\u7ebf\u7a0b\u662f\u4e0d\u53ef\u89c1\u7684\u3002\u8fd9\u6837\uff0c\u6bcf\u4e2a\u7ebf\u7a0b\u6700\u7ec8\u53ef\u80fd\u4f1a\u62e5\u6709\u81ea\u5df1\u7684\u5171\u4eab\u5bf9\u8c61\u526f\u672c\uff0c\u6bcf\u4e2a\u526f\u672c\u4f4d\u4e8e\u4e0d\u540c\u7684 CPU \u7f13\u5b58\u4e2d\u3002<\/p>\n\n\n\n<p>\u4e0b\u56fe\u8bf4\u660e\u4e86\u5927\u6982\u7684\u60c5\u51b5\u3002\u5de6\u4fa7 CPU \u4e0a\u8fd0\u884c\u7684\u4e00\u4e2a\u7ebf\u7a0b\u5c06\u5171\u4eab\u5bf9\u8c61\u590d\u5236\u5230\u5176 CPU \u7f13\u5b58\u4e2d\uff0c\u5e76\u5c06\u5176 <code>count<\/code> \u53d8\u91cf\u66f4\u6539\u4e3a 2\u3002\u6b64\u66f4\u6539\u5bf9\u53f3\u4fa7 CPU \u4e0a\u8fd0\u884c\u7684\u5176\u4ed6\u7ebf\u7a0b\u4e0d\u53ef\u89c1\uff0c\u56e0\u4e3a <code>count<\/code> \u503c\u5c1a\u672a\u66f4\u65b0\u5237\u65b0\u56de\u4e3b\u5185\u5b58.<\/p>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/jenkov.com\/images\/java-concurrency\/java-memory-model-6.png\" alt=\"Java \u5185\u5b58\u6a21\u578b\u4e2d\u7684\u53ef\u89c1\u6027\u95ee\u9898\u3002\" width=\"390\" height=\"295\"\/><\/figure>\n\n\n\n<p>\u8981\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 <a href=\"http:\/\/codermr.com\/index.php\/2022\/11\/03\/whatis_volatile\/\" data-type=\"URL\" data-id=\"http:\/\/codermr.com\/index.php\/2022\/11\/03\/whatis_volatile\/\">Java \u7684 volatile \u5173\u952e\u5b57<\/a>\u3002\u8be5<code>volatile<\/code>&nbsp;\u5173\u952e\u5b57\u53ef\u4ee5\u786e\u4fdd\u7ed9\u5b9a\u53d8\u91cf\u76f4\u63a5\u4ece\u4e3b\u5b58\u4e2d\u8bfb\u53d6\uff0c\u5e76\u5728\u66f4\u65b0\u65f6\u59cb\u7ec8\u5199\u56de\u4e3b\u5b58\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u7ade\u4e89\u6761\u4ef6<\/h3>\n\n\n\n<p>\u5982\u679c\u4e24\u4e2a\u6216\u591a\u4e2a\u7ebf\u7a0b\u5171\u4eab\u4e00\u4e2a\u5bf9\u8c61\uff0c\u5e76\u4e14\u591a\u4e2a\u7ebf\u7a0b\u66f4\u65b0\u8be5\u5171\u4eab\u5bf9\u8c61\u4e2d\u7684\u53d8\u91cf\uff0c \u5219\u53ef\u80fd\u4f1a\u51fa\u73b0\u7ade\u4e89\u6761\u4ef6\u3002<\/p>\n\n\n\n<p>\u60f3\u8c61\u4e00\u79cd\u573a\u666f\uff0c\u7ebf\u7a0b A \u5c06 <code>count<\/code> \u8fd9\u4e2a\u5171\u4eab\u53d8\u91cf\u8bfb\u5165\u5b83\u7684 CPU \u7f13\u5b58\u4e2d\uff0c\u7ebf\u7a0b B \u4e5f\u505a\u4e86\u540c\u6837\u7684\u4e8b\u60c5\uff0c\u4f46\u8fdb\u5165\u4e0d\u540c\u7684 CPU \u7f13\u5b58\u3002\u73b0\u5728\u7ebf\u7a0b A \u5c06 <code>count<\/code> \u503c\u52a01\uff0c\u7ebf\u7a0b B \u4e5f\u8fd9\u6837\u505a\u3002\u73b0\u5728 <code><code>count<\/code> <\/code>\u503c\u5df2\u7ecf\u88ab\u589e\u52a0\u4e86\u4e24\u6b21\uff0c\u5982\u679c\u8fd9\u4e9b\u589e\u91cf\u662f\u6309\u987a\u5e8f\u6267\u884c\u7684\uff0c\u5219\u53d8\u91cf<code>count<\/code>\u5c06\u589e\u52a0\u4e24\u6b21\uff0c\u5c06\u539f\u59cb\u503c\u52a0 2 \u7136\u540e\u5199\u56de\u4e3b\u5b58\u50a8\u5668\u3002<\/p>\n\n\n\n<p>\u7136\u800c\uff0c\u8fd9\u4e24\u6b21\u5bf9 <code><code>count<\/code> <\/code>\u503c\u7684\u589e\u52a0\u662f\u5728\u6ca1\u6709\u540c\u6b65\u7684\u60c5\u51b5\u4e0b\u540c\u65f6\u8fdb\u884c\u7684\u3002\u65e0\u8bba\u7ebf\u7a0b A \u548c B \u4e2d\u7684\u54ea\u4e2a\u7ebf\u7a0b\u5c06\u5176\u66f4\u65b0\u7248\u672c\u7684 <code>count<\/code> \u503c\u5199\u56de\u4e3b\u5185\u5b58\uff0c\u66f4\u65b0\u540e\u7684\u503c\u53ea\u4f1a\u6bd4\u539f\u59cb\u503c\u5927 1\uff0c\u5c3d\u7ba1\u7a0b\u5e8f\u4e2d\u662f\u6709\u4e24\u6b21\u5bf9 <code>count<\/code> \u503c\u52a01\u3002<\/p>\n\n\n\n<p>\u8981\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u53ef\u4ee5\u4f7f\u7528 Java \u540c\u6b65\u5757\u3002\u540c\u6b65\u5757\u4fdd\u8bc1\u4e86\u5728\u4efb\u4f55\u7ed9\u5b9a\u65f6\u95f4\u91cc\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u53ef\u4ee5\u8fdb\u5165\u4ee3\u7801\u4e2d\u7684\u6307\u5b9a\u90e8\u5206\u3002\u540c\u6b65\u5757\u8fd8\u4fdd\u8bc1\u5728\u540c\u6b65\u5757\u5185\u8bbf\u95ee\u7684\u6240\u6709\u53d8\u91cf\u90fd\u4f1a\u4ece<strong>\u4e3b\u5b58<\/strong>\u4e2d\u8bfb\u5165\uff0c\u5e76\u4e14\u5f53\u7ebf\u7a0b\u9000\u51fa\u540c\u6b65\u5757\u65f6\uff0c\u6240\u6709\u66f4\u65b0\u7684\u53d8\u91cf\u90fd\u4f1a\u518d\u6b21\u5237\u65b0\u56de\u4e3b\u5b58\uff0c\u65e0\u8bba\u8be5\u53d8\u91cf\u662f\u58f0\u660e\u4e3a volatile \u8fd8\u662f\u4e0d\u662f\u3002<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">\u5b8c\u3002<\/h1>\n","protected":false},"excerpt":{"rendered":"<p>\u6211\u4eec\u77e5\u9053\u865a\u62df\u673a\uff08Virtual Machine\uff09\u662f\u6307\u901a\u8fc7\u8f6f\u4ef6\u6a21\u62df\u7684\u5177\u6709\u5b8c\u6574\u786c\u4ef6\u7cfb\u7edf\u529f\u80fd\u7684\u3001\u8fd0\u884c\u5728\u4e00\u4e2a\u5b8c\u5168\u9694\u79bb\u73af&#8230;<\/p>\n","protected":false},"author":1,"featured_media":739,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[8,11],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Java \u5185\u5b58\u6a21\u578b - \u7801\u5148\u751f\u7684\u535a\u5ba2<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/codermr.com\/index.php\/2022\/11\/05\/talk_jmm\/\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java \u5185\u5b58\u6a21\u578b - \u7801\u5148\u751f\u7684\u535a\u5ba2\" \/>\n<meta property=\"og:description\" content=\"\u6211\u4eec\u77e5\u9053\u865a\u62df\u673a\uff08Virtual Machine\uff09\u662f\u6307\u901a\u8fc7\u8f6f\u4ef6\u6a21\u62df\u7684\u5177\u6709\u5b8c\u6574\u786c\u4ef6\u7cfb\u7edf\u529f\u80fd\u7684\u3001\u8fd0\u884c\u5728\u4e00\u4e2a\u5b8c\u5168\u9694\u79bb\u73af...\" \/>\n<meta property=\"og:url\" content=\"http:\/\/codermr.com\/index.php\/2022\/11\/05\/talk_jmm\/\" \/>\n<meta property=\"og:site_name\" content=\"\u7801\u5148\u751f\u7684\u535a\u5ba2\" \/>\n<meta property=\"article:published_time\" content=\"2022-11-05T15:09:53+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-03-25T15:34:32+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/codermr.com\/wp-content\/uploads\/2022\/11\/jvm2.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1500\" \/>\n\t<meta property=\"og:image:height\" content=\"1371\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"\u7801\u5148\u751f\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/mrcode2021\" \/>\n<meta name=\"twitter:site\" content=\"@mrcode2021\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"\u7801\u5148\u751f\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"http:\/\/codermr.com\/index.php\/2022\/11\/05\/talk_jmm\/#article\",\"isPartOf\":{\"@id\":\"http:\/\/codermr.com\/index.php\/2022\/11\/05\/talk_jmm\/\"},\"author\":{\"name\":\"\u7801\u5148\u751f\",\"@id\":\"http:\/\/codermr.com\/#\/schema\/person\/39016e15c79e4f02d1ed3a64688619bf\"},\"headline\":\"Java \u5185\u5b58\u6a21\u578b\",\"datePublished\":\"2022-11-05T15:09:53+00:00\",\"dateModified\":\"2023-03-25T15:34:32+00:00\",\"mainEntityOfPage\":{\"@id\":\"http:\/\/codermr.com\/index.php\/2022\/11\/05\/talk_jmm\/\"},\"wordCount\":82,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/codermr.com\/#\/schema\/person\/39016e15c79e4f02d1ed3a64688619bf\"},\"keywords\":[\"JVM\",\"\u7ebf\u7a0b\u5b89\u5168\"],\"articleSection\":[\"JAVA\"],\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"http:\/\/codermr.com\/index.php\/2022\/11\/05\/talk_jmm\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"http:\/\/codermr.com\/index.php\/2022\/11\/05\/talk_jmm\/\",\"url\":\"http:\/\/codermr.com\/index.php\/2022\/11\/05\/talk_jmm\/\",\"name\":\"Java \u5185\u5b58\u6a21\u578b - \u7801\u5148\u751f\u7684\u535a\u5ba2\",\"isPartOf\":{\"@id\":\"http:\/\/codermr.com\/#website\"},\"datePublished\":\"2022-11-05T15:09:53+00:00\",\"dateModified\":\"2023-03-25T15:34:32+00:00\",\"breadcrumb\":{\"@id\":\"http:\/\/codermr.com\/index.php\/2022\/11\/05\/talk_jmm\/#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/codermr.com\/index.php\/2022\/11\/05\/talk_jmm\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/codermr.com\/index.php\/2022\/11\/05\/talk_jmm\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"http:\/\/codermr.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java \u5185\u5b58\u6a21\u578b\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/codermr.com\/#website\",\"url\":\"http:\/\/codermr.com\/\",\"name\":\"\u7801\u5148\u751f\u7684\u535a\u5ba2\",\"description\":\"\u6b22 \u8fce \u4e0b \u8f7d \u6211 \u5f00 \u53d1 \u7684 \u5404 \u7aef \u8f6f \u4ef6 \u548c APP\",\"publisher\":{\"@id\":\"http:\/\/codermr.com\/#\/schema\/person\/39016e15c79e4f02d1ed3a64688619bf\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/codermr.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"http:\/\/codermr.com\/#\/schema\/person\/39016e15c79e4f02d1ed3a64688619bf\",\"name\":\"\u7801\u5148\u751f\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"http:\/\/codermr.com\/#\/schema\/person\/image\/\",\"url\":\"http:\/\/codermr.com\/wp-content\/uploads\/2023\/02\/wukong.jpg\",\"contentUrl\":\"http:\/\/codermr.com\/wp-content\/uploads\/2023\/02\/wukong.jpg\",\"width\":400,\"height\":400,\"caption\":\"\u7801\u5148\u751f\"},\"logo\":{\"@id\":\"http:\/\/codermr.com\/#\/schema\/person\/image\/\"},\"sameAs\":[\"http:\/\/codermr.com\",\"https:\/\/twitter.com\/https:\/\/twitter.com\/mrcode2021\"],\"url\":\"http:\/\/codermr.com\/index.php\/author\/coderma\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Java \u5185\u5b58\u6a21\u578b - \u7801\u5148\u751f\u7684\u535a\u5ba2","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"http:\/\/codermr.com\/index.php\/2022\/11\/05\/talk_jmm\/","og_locale":"zh_CN","og_type":"article","og_title":"Java \u5185\u5b58\u6a21\u578b - \u7801\u5148\u751f\u7684\u535a\u5ba2","og_description":"\u6211\u4eec\u77e5\u9053\u865a\u62df\u673a\uff08Virtual Machine\uff09\u662f\u6307\u901a\u8fc7\u8f6f\u4ef6\u6a21\u62df\u7684\u5177\u6709\u5b8c\u6574\u786c\u4ef6\u7cfb\u7edf\u529f\u80fd\u7684\u3001\u8fd0\u884c\u5728\u4e00\u4e2a\u5b8c\u5168\u9694\u79bb\u73af...","og_url":"http:\/\/codermr.com\/index.php\/2022\/11\/05\/talk_jmm\/","og_site_name":"\u7801\u5148\u751f\u7684\u535a\u5ba2","article_published_time":"2022-11-05T15:09:53+00:00","article_modified_time":"2023-03-25T15:34:32+00:00","og_image":[{"width":1500,"height":1371,"url":"http:\/\/codermr.com\/wp-content\/uploads\/2022\/11\/jvm2.jpg","type":"image\/jpeg"}],"author":"\u7801\u5148\u751f","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/mrcode2021","twitter_site":"@mrcode2021","twitter_misc":{"\u4f5c\u8005":"\u7801\u5148\u751f","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"2 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"http:\/\/codermr.com\/index.php\/2022\/11\/05\/talk_jmm\/#article","isPartOf":{"@id":"http:\/\/codermr.com\/index.php\/2022\/11\/05\/talk_jmm\/"},"author":{"name":"\u7801\u5148\u751f","@id":"http:\/\/codermr.com\/#\/schema\/person\/39016e15c79e4f02d1ed3a64688619bf"},"headline":"Java \u5185\u5b58\u6a21\u578b","datePublished":"2022-11-05T15:09:53+00:00","dateModified":"2023-03-25T15:34:32+00:00","mainEntityOfPage":{"@id":"http:\/\/codermr.com\/index.php\/2022\/11\/05\/talk_jmm\/"},"wordCount":82,"commentCount":0,"publisher":{"@id":"http:\/\/codermr.com\/#\/schema\/person\/39016e15c79e4f02d1ed3a64688619bf"},"keywords":["JVM","\u7ebf\u7a0b\u5b89\u5168"],"articleSection":["JAVA"],"inLanguage":"zh-Hans","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["http:\/\/codermr.com\/index.php\/2022\/11\/05\/talk_jmm\/#respond"]}]},{"@type":"WebPage","@id":"http:\/\/codermr.com\/index.php\/2022\/11\/05\/talk_jmm\/","url":"http:\/\/codermr.com\/index.php\/2022\/11\/05\/talk_jmm\/","name":"Java \u5185\u5b58\u6a21\u578b - \u7801\u5148\u751f\u7684\u535a\u5ba2","isPartOf":{"@id":"http:\/\/codermr.com\/#website"},"datePublished":"2022-11-05T15:09:53+00:00","dateModified":"2023-03-25T15:34:32+00:00","breadcrumb":{"@id":"http:\/\/codermr.com\/index.php\/2022\/11\/05\/talk_jmm\/#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["http:\/\/codermr.com\/index.php\/2022\/11\/05\/talk_jmm\/"]}]},{"@type":"BreadcrumbList","@id":"http:\/\/codermr.com\/index.php\/2022\/11\/05\/talk_jmm\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"http:\/\/codermr.com\/"},{"@type":"ListItem","position":2,"name":"Java \u5185\u5b58\u6a21\u578b"}]},{"@type":"WebSite","@id":"http:\/\/codermr.com\/#website","url":"http:\/\/codermr.com\/","name":"\u7801\u5148\u751f\u7684\u535a\u5ba2","description":"\u6b22 \u8fce \u4e0b \u8f7d \u6211 \u5f00 \u53d1 \u7684 \u5404 \u7aef \u8f6f \u4ef6 \u548c APP","publisher":{"@id":"http:\/\/codermr.com\/#\/schema\/person\/39016e15c79e4f02d1ed3a64688619bf"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/codermr.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":["Person","Organization"],"@id":"http:\/\/codermr.com\/#\/schema\/person\/39016e15c79e4f02d1ed3a64688619bf","name":"\u7801\u5148\u751f","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"http:\/\/codermr.com\/#\/schema\/person\/image\/","url":"http:\/\/codermr.com\/wp-content\/uploads\/2023\/02\/wukong.jpg","contentUrl":"http:\/\/codermr.com\/wp-content\/uploads\/2023\/02\/wukong.jpg","width":400,"height":400,"caption":"\u7801\u5148\u751f"},"logo":{"@id":"http:\/\/codermr.com\/#\/schema\/person\/image\/"},"sameAs":["http:\/\/codermr.com","https:\/\/twitter.com\/https:\/\/twitter.com\/mrcode2021"],"url":"http:\/\/codermr.com\/index.php\/author\/coderma\/"}]}},"_links":{"self":[{"href":"http:\/\/codermr.com\/index.php\/wp-json\/wp\/v2\/posts\/17"}],"collection":[{"href":"http:\/\/codermr.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/codermr.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/codermr.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/codermr.com\/index.php\/wp-json\/wp\/v2\/comments?post=17"}],"version-history":[{"count":10,"href":"http:\/\/codermr.com\/index.php\/wp-json\/wp\/v2\/posts\/17\/revisions"}],"predecessor-version":[{"id":854,"href":"http:\/\/codermr.com\/index.php\/wp-json\/wp\/v2\/posts\/17\/revisions\/854"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/codermr.com\/index.php\/wp-json\/wp\/v2\/media\/739"}],"wp:attachment":[{"href":"http:\/\/codermr.com\/index.php\/wp-json\/wp\/v2\/media?parent=17"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/codermr.com\/index.php\/wp-json\/wp\/v2\/categories?post=17"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/codermr.com\/index.php\/wp-json\/wp\/v2\/tags?post=17"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}