{"id":11,"date":"2022-11-03T23:17:18","date_gmt":"2022-11-03T15:17:18","guid":{"rendered":"http:\/\/codermr.com\/?p=11"},"modified":"2023-02-18T09:13:37","modified_gmt":"2023-02-18T01:13:37","slug":"whatis_volatile","status":"publish","type":"post","link":"http:\/\/codermr.com\/index.php\/2022\/11\/03\/whatis_volatile\/","title":{"rendered":"volatile \u5173\u952e\u5b57\u8be6\u89e3"},"content":{"rendered":"\n<p>\u672c\u6587\uff0c\u6211\u4f1a\u4ece\u300e\u7f13\u5b58\u300f\u3001\u300eCPU \u9ad8\u901f\u7f13\u5b58\u300f\u8bf4\u8d77\uff0c\u7136\u540e\u7ed3\u5408 JMM \u6765\u5e2e\u52a9\u5927\u5bb6\u9010\u6b65\u5b66\u4e60\u548c\u7406\u89e3 volatile \u7684\u7528\u6cd5\u548c\u539f\u7406\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u7f13\u5b58<\/h1>\n\n\n\n<p>\u505a\u8fc7\u540e\u7aef\u5f00\u53d1\u7684\u540c\u5b66\u90fd\u4e00\u5b9a\u542c\u8bf4\u8fc7 Redis \u3001Memcache \u8fd9\u4e9b\u7f13\u5b58\uff0c\u4e5f\u7ecf\u5e38\u4f7f\u7528\uff0c\u90a3\u4e48\u4e3a\u4ec0\u4e48\u9700\u8981\u7f13\u5b58\u5462\uff1f<\/p>\n\n\n\n<p>\u56e0\u4e3a\u6570\u636e\u5e93\u8bfb\u5199\u90fd\u8981\u64cd\u4f5c\u78c1\u76d8\uff0c\u800c\u78c1\u76d8\u7684\u8bfb\u5199\u901f\u5ea6\u548c\u5185\u5b58\u7684\u8bfb\u5199\u901f\u5ea6\u5dee\u522b\u592a\u5927\u4e86\uff0c\u5728\u9ad8\u5e76\u53d1\u60c5\u51b5\u4e0b\uff0c\u6570\u636e\u5e93\u5f88\u53ef\u80fd\u4f1a\u9996\u5148\u6210\u4e3a\u6027\u80fd\u74f6\u9888\uff08\u867d\u7136\u5f88\u591a\u516c\u53f8\u7684\u4e1a\u52a1\u5e76\u6ca1\u6709\u4ec0\u4e48\u5e76\u53d1\u91cf\uff09\u3002\u6240\u4ee5 Redis \u3001Memcache \u8fd9\u4e9b\u7f13\u5b58\u90fd\u662f\u4e3a\u4e86\u63d0\u9ad8\u6570\u636e\u7684\u8bfb\u5199\u901f\u5ea6\uff0c\u8fdb\u800c\u63d0\u9ad8\u7f51\u7ad9\u54cd\u5e94\u901f\u5ea6\u7684\uff0c\u4f46\u5fc5\u7136\u8981\u9762\u4e34\u7684\u4e00\u4e2a\u95ee\u9898\u662f\u300e\u7f13\u5b58\u4e00\u81f4\u6027\u300f\uff0c\u5373\u7f13\u5b58\u4e2d\u6570\u636e\u548c\u6570\u636e\u5e93\u4e2d\u6570\u636e\u7684\u4e00\u81f4\u6027\u95ee\u9898\u3002\u672c\u6587\u4e0d\u518d\u8d58\u8ff0\uff0c\u89c1\u6211\u7684\u53e6\u4e00\u7bc7\u535a\u5ba2\u3002<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">CPU \u9ad8\u901f\u7f13\u5b58<\/h1>\n\n\n\n<p>CPU \u4e5f\u6709\u7f13\u5b58\uff0c\u524d\u9762\u8bf4\u4e86\u300e\u78c1\u76d8\u7684\u8bfb\u5199\u901f\u5ea6\u548c\u5185\u5b58\u7684\u8bfb\u5199\u901f\u5ea6\u5dee\u522b\u5de8\u5927\u300f\uff0c\u800c\u5185\u5b58\u7684\u8bfb\u5199\u901f\u5ea6\u548c CPU \u6bd4\u8d77\u6765\u7684\u4e5f\u662f\u5dee\u522b\u5de8\u5927\uff0c\u6240\u4ee5 CPU \u4e5f\u6709\u5b83\u81ea\u5df1\u7684\u7f13\u5b58\uff0c\u5177\u4f53\u6765\u8bf4\u6709\u4e00\u7ea7\u7f13\u5b58\uff0c\u4e8c\u7ea7\u7f13\u5b58\uff0c\u4e09\u7ea7\u7f13\u5b58\uff0c\u6709\u7684\u7f13\u5b58\u5728 CPU \u5185\u90e8\uff0c\u6709\u7684\u5728 CPU \u5916\u90e8\uff0c\u4e0d\u8fc7\u5bf9\u4e8e\u672c\u6587\u6211\u60f3\u8bb2\u8ff0\u7684\u95ee\u9898\u6765\u8bf4\u8fd9\u4e0d\u91cd\u8981\uff0c\u53ea\u7528\u77e5\u9053\uff0c<mark style=\"background-color:rgba(0, 0, 0, 0);color:#4169e1\" class=\"has-inline-color\">CPU \u8bfb\u5199\u6570\u636e\u65f6\u4f1a\u5148\u68c0\u67e5\u6570\u636e\u662f\u5426\u5728 CPU \u9ad8\u901f\u7f13\u5b58\u4e2d\uff0c\u5982\u679c\u5728\u5c31\u5bf9\u5176\u8fdb\u884c\u8bfb\u5199\uff0c\u518d\u5728\u5408\u9002\u7684\u65f6\u673a\u5c06\u6570\u636e\u5237\u65b0\u56de\u4e3b\u5b58\uff1b\u5982\u679c\u4e0d\u5728\u5219 CPU \u5c31\u4f1a\u8bbf\u95ee\u4e3b\u5b58\u7136\u540e\u518d\u5c06\u6570\u636e\u653e\u8fdb CPU \u9ad8\u901f\u7f13\u5b58\u4e2d<\/mark>\u3002\u6240\u4ee5\u5bf9\u4e8e CPU \u7f13\u5b58\u4e2d\u7684\u540c\u4e00\u4e2a\u6570\u636e\uff0c\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u8bfb\u53d6\u5e76\u8ba1\u7b97\u7684\u65f6\u5019\uff0c\u5c31\u53ef\u80fd\u4f1a\u51fa\u73b0\u6570\u636e\u51c6\u786e\u6027\u7684\u95ee\u9898\u3002\uff08\u6bd4\u5982 CPU \u7f13\u5b58\u4e2d\u7684\u6570\u636e i=0\uff0c\u4e24\u4e2a\u7ebf\u7a0b\u540c\u65f6\u8bfb\u53d6\u5230 i=0\u5e76\u8fdb\u884c\u81ea\u589e\uff0c\u6700\u540e\u7ed3\u679c i=1\uff0c\u8fd9\u663e\u793a\u662f\u9519\u8bef\u7684\uff1b\u53c8\u6bd4\u5982\u5bf9\u4e8e\u540c\u4e00\u4e2a\u6570\u636e\uff0c\u591a\u4e2a\u7ebf\u7a0b\u90fd\u5bf9\u5176\u8fdb\u884c\u4e86\u66f4\u65b0\u5e76\u5c06\u66f4\u65b0\u540e\u7684\u6570\u636e\u653e\u8fdb\u4e86\u4e0d\u540c\u7684 CPU \u9ad8\u901f\u7f13\u5b58\u4e2d\uff0c\u5219\u8be5\u6570\u636e\u5728\u4ece CPU \u9ad8\u901f\u7f13\u5b58\u5237\u65b0\u56de\u4e3b\u5b58\u65f6\u5c31\u6709\u53ef\u80fd\u4e0d\u51c6\u786e\uff09\u3002<\/p>\n\n\n\n<p>\u4e3a\u4e86\u89e3\u51b3 CPU \u7f13\u5b58\u5bfc\u81f4\u7684\u7f13\u5b58\u4e00\u81f4\u6027\u95ee\u9898\uff0c\u6709\u5173\u90e8\u95e8\u5236\u5b9a\u4e86\u300e\u7f13\u5b58\u4e00\u81f4\u6027\u534f\u8bae\u300f\uff0c\u4e3b\u8981\u662f\u5236\u5b9a CPU \u7f13\u5b58\u4e0e\u4e3b\u5b58\u4ea4\u4e92\u65f6\u7684\u89c4\u8303\u3002\u4e0d\u540c\u7684 CPU \u5176\u7f13\u5b58\u4e00\u81f4\u6027\u534f\u8bae\u4e5f\u4e0d\u540c\u3002\u800c\u64cd\u4f5c\u7cfb\u7edf\u6211\u4eec\u77e5\u9053\uff0c\u5b83\u5c4f\u853d\u4e86\u5e95\u5c42\u786c\u4ef6\u7684\u64cd\u4f5c\u7ec6\u8282\uff0c\u5b83\u9700\u8981\u63d0\u4f9b\u63a5\u53e3\u7ed9\u5916\u90e8\u7a0b\u5e8f\u8c03\u7528\uff0c\u6240\u4ee5\u64cd\u4f5c\u7cfb\u7edf\u4e5f\u8981\u5b9a\u4e49\u4e00\u5957\u89c4\u8303\uff0c\u6765\u89e3\u51b3 CPU \u7f13\u5b58\u6570\u636e\u4e00\u81f4\u6027\u7684\u95ee\u9898\uff0c\u800c\u300e\u5185\u5b58\u6a21\u578b\u300f\u5c31\u662f\u8fd9\u6837\u4e00\u5957\u89c4\u8303\uff0c\u800c\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u5982 Linux \u548c Windows \u7684\u300e\u5185\u5b58\u6a21\u578b\u300f\u80af\u5b9a\u662f\u4e0d\u4e00\u6837\u7684\u3002<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">JVM \u5185\u5b58\u6a21\u578b\uff08JMM\uff09<\/h1>\n\n\n\n<p>JVM \u662f\u8de8\u5e73\u53f0\u7684\u865a\u62df\u673a\uff0c\u800c\u865a\u62df\u673a\u6211\u4eec\u77e5\u9053\uff0c\u662f\u901a\u8fc7\u8f6f\u4ef6\u6a21\u62df\u7684\u5177\u6709\u5b8c\u6574\u786c\u4ef6\u7cfb\u7edf\u529f\u80fd\u7684\u8ba1\u7b97\u673a\u7cfb\u7edf\uff0c\u6240\u4ee5 JVM \u6709\u5b83\u81ea\u5df1\u7684\u300e\u5185\u5b58\u6a21\u578b\u300f\u5373 JMM \uff0c<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">JMM \u89c4\u5b9a\u4e86\u7ebf\u7a0b\u4e4b\u95f4\u7684\u5171\u4eab\u53d8\u91cf\u8981\u4fdd\u5b58\u5728\u300e\u4e3b\u5b58\u300f\u5f53\u4e2d\uff0c\u6bcf\u4e2a\u7ebf\u7a0b\u6709\u81ea\u5df1\u7684\u300e\u5de5\u4f5c\u5185\u5b58\u300f\uff08\u62bd\u8c61\u6982\u5ff5\uff09\uff0c\u6bcf\u4e2a\u7ebf\u7a0b\u66f4\u65b0\u5171\u4eab\u53d8\u91cf\u65f6\u9700\u8981\u5148\u5c06\u5171\u4eab\u53d8\u91cf\u4ece \u300e\u4e3b\u5b58\u300f \u4e2d\u8bfb\u8fdb\u7ebf\u7a0b\u81ea\u5df1\u7684\u300e\u5de5\u4f5c\u5185\u5b58\u300f\uff0c\u5728\u300e\u5de5\u4f5c\u5185\u5b58\u300f\u4e2d\u66f4\u65b0\u5b8c\u4e4b\u540e\u518d\u5199\u56de\u300e\u4e3b\u5b58\u300f\u4e2d\u3002<\/mark>\u4e8e\u662f\u4e4e\uff0c\u8fd9\u4e5f\u4ea7\u751f\u4e86\u7ebf\u7a0b\u5b89\u5168\u7684\u95ee\u9898\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class MyThread extends Thread {\n     public static int num = 0;\n     @Override\n     public void run() {\n         for (int i = 0; i &lt; 10000; i++) {\n             num++;\n         }\n     }\n }<\/code><\/pre>\n\n\n\n<p>\u4e0a\u9762\u7684\u7ebf\u7a0b\u7c7b\uff0c\u4e0d\u7528\u521b\u5efa\u5f88\u591a\u4e2a\u5bf9\u8c61\uff0c\u53ea\u7528\u521b\u5efa\u4e24\u4e2a MyThread \u5bf9\u8c61\u5e76\u8fd0\u884c\uff0c\u5c31\u80fd\u770b\u5230\u7ebf\u7a0b\u5b89\u5168\u7684\u95ee\u9898\uff0cnum \u6700\u7ec8\u7684\u503c\u5e76\u4e0d\u662f\u671f\u671b\u7684 20000 \u3002\u539f\u56e0\u5c31\u662f\u56e0\u4e3a\u5171\u4eab\u53d8\u91cf num \u662f\u4fdd\u5b58\u5728\u300e\u4e3b\u5b58\u300f\u4e2d\u7684\uff0c\u6bcf\u4e2a\u7ebf\u7a0b\u90fd\u4f1a\u4ece\u300e\u4e3b\u5b58\u300f\u4e2d\u8bfb\u53d6 num \u7684\u503c\u5230\u81ea\u5df1\u7684\u300e\u5de5\u4f5c\u5185\u5b58\u300f\u5f53\u4e2d\u8fdb\u884c\u81ea\u589e\uff0c\u7136\u540e\u5c06\u503c\u518d\u5199\u56de\u300e\u4e3b\u5b58\u300f\u4e4b\u4e2d\uff0c\u8fd9\u5c31\u5fc5\u7136\u4f1a\u5bfc\u81f4\u7ebf\u7a0b\u5b89\u5168\u7684\u95ee\u9898\u3002<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">volatile \u7684\u542b\u4e49<\/h1>\n\n\n\n<p>\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c\u5982\u679c\u7528 volatile \u4fee\u9970\u53d8\u91cf num \u662f\u5426\u80fd\u89e3\u51b3\u7ebf\u7a0b\u5b89\u5168\u7684\u95ee\u9898\u5462\uff0c\u4f60\u53ef\u4ee5\u8bd5\u4e00\u4e0b\uff0c\u4f1a\u53d1\u73b0\u7528 volatile \u4fee\u9970 num \u4f9d\u7136\u5b58\u5728\u7ebf\u7a0b\u5b89\u5168\u7684\u95ee\u9898\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u90a3\u4e48 volatile \u5230\u5e95\u662f\u4ec0\u4e48\u4f5c\u7528\u5462\uff1f<\/h3>\n\n\n\n<p>\u7b80\u800c\u8a00\u4e4b\uff0c<mark style=\"background-color:rgba(0, 0, 0, 0);color:#4169e1\" class=\"has-inline-color\">\u7528 volatile \u4fee\u9970\u4e86 num \uff0c\u5219\u8be5 num \u53d8\u91cf\u4f1a\u88ab\u6807\u8bb0\u4e3a\u5b58\u50a8\u5728\u300e\u4e3b\u5b58\u300f\u4e2d<\/mark>\uff0c\u6bcf\u4e2a\u7ebf\u7a0b\u90fd\u4f1a\u76f4\u63a5\u8bfb\u53d6\u5e76\u4fee\u6539\u300e\u4e3b\u5b58\u300f\u4e2d\u7684 num \u7684\u503c\uff0c\u4fdd\u8bc1\u4e86\u4e0d\u540c\u7ebf\u7a0b\u4e2d\u8be5\u53d8\u91cf\u7684\u53ef\u89c1\u6027\u3002\uff08\u4f46\u8fd9\u5e76\u4e0d\u80fd\u4fdd\u8bc1\u7ebf\u7a0b\u5b89\u5168\uff0c\u5982\u4e0a\u9762\u4f8b\u5b50\u6240\u793a\u4ecd\u7136\u4f1a\u5b58\u5728\u7ebf\u7a0b\u5b89\u5168\u7684\u95ee\u9898\uff09<\/p>\n\n\n\n<p>\u90a3\u4e48 volatile \u9002\u7528\u4e8e\u4ec0\u4e48\u573a\u666f\u5462\uff1f\u4e00\u822c\u7b26\u5408\u4e0b\u9762\u4e24\u79cd\u573a\u666f\uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528 volatile \uff1a<\/p>\n\n\n\n<ul>\n<li>\u53d8\u91cf\u7684\u8fd0\u7b97\u7ed3\u679c\u5e76\u4e0d\u4f9d\u8d56\u4e8e\u53d8\u91cf\u7684\u5f53\u524d\u503c\uff0c\u6216\u80fd\u591f\u4fdd\u8bc1\u53ea\u6709\u5355\u4e00\u7684\u7ebf\u7a0b\u4fee\u6539\u53d8\u91cf\u7684\u503c\u3002<\/li>\n\n\n\n<li>\u53d8\u91cf\u4e0d\u9700\u8981\u4e0e\u5176\u4ed6\u7684\u72b6\u6001\u53d8\u91cf\u5171\u540c\u53c2\u4e0e\u4e0d\u53d8\u7ea6\u675f\u3002<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p>\u518d\u5177\u4f53\u4e00\u70b9\u6765\u8bf4\uff0c\u5c31\u662f volatile \u4fee\u9970\u7684\u53d8\u91cf\uff0c\u4e0d\u80fd\u5728\u8be5\u53d8\u91cf\u7684\u57fa\u7840\u4e0a\u5bf9\u8be5\u53d8\u91cf\u8fdb\u884c\u4fee\u6539\uff08\u6bd4\u5982 i++ \u5c31\u662f\u5728 i \u503c\u7684\u57fa\u7840\u4e0a\u8fdb\u884c\u4fee\u6539\uff09\u3002volatile \u901a\u5e38\u7684\u7528\u6cd5\u662f\uff0c\u76f4\u63a5\u5728\u65b9\u6cd5\u5185\u90e8\u7ed9 volatile \u53d8\u91cf\u8d4b\u503c\uff0c\u6bd4\u5982 JDK \u6e90\u7801\u4e2d ConcurrentLinkedQueue \u7684\u6e90\u7801\u91cc\u9762\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/codermr.com\/wp-content\/uploads\/2023\/01\/image-4-1024x444-1.png\" alt=\"\" class=\"wp-image-141\" width=\"512\" height=\"222\" srcset=\"http:\/\/codermr.com\/wp-content\/uploads\/2023\/01\/image-4-1024x444-1.png 1024w, http:\/\/codermr.com\/wp-content\/uploads\/2023\/01\/image-4-1024x444-1-300x130.png 300w, http:\/\/codermr.com\/wp-content\/uploads\/2023\/01\/image-4-1024x444-1-768x333.png 768w, http:\/\/codermr.com\/wp-content\/uploads\/2023\/01\/image-4-1024x444-1-816x354.png 816w\" sizes=\"(max-width: 512px) 100vw, 512px\" \/><figcaption class=\"wp-element-caption\">ConcurrentLinkedQueue \u6e90\u7801\u4e2d volatile \u7684\u7528\u6cd5<\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">\u518d\u8bf4 volatile \u7684\u53ef\u89c1\u6027<\/h3>\n\n\n\n<p>\u4e0a\u9762\u8bf4\u4e86\uff0c\u88ab volatile \u4fee\u9970\u7684\u53d8\u91cf\uff0c\u80fd\u591f\u5728\u4e0d\u540c\u7ebf\u7a0b\u4e4b\u95f4\u4fdd\u6301\u53ef\u89c1\u6027\u3002\u4f46\u662f\uff0c\u4ec5\u4ec5\u662f\u88ab volatile \u4fee\u9970\u7684\u8fd9\u4e2a\u53d8\u91cf\u624d\u80fd\u5728\u4e0d\u540c\u7ebf\u7a0b\u4e4b\u95f4\u4fdd\u6301\u53ef\u89c1\u6027\u5417\uff1f<\/p>\n\n\n\n<p>\u5e76\u4e0d\u662f\u3002<\/p>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">volatile \u7684\u53ef\u89c1\u6027\u4fdd\u8bc1\u5982\u4e0b\uff1a<\/mark><\/p>\n\n\n\n<ul>\n<li><strong>\u3010\u53ef\u89c1\u6027\u5b9a\u7406\u4e00\u3011<\/strong>\u5982\u679c\u7ebf\u7a0b A <strong>\u5199\u5165<\/strong>\u4e00\u4e2a volatile \u53d8\u91cf\uff0c\u800c\u7ebf\u7a0b B \u968f\u540e\u8bfb\u53d6\u4e86\u540c\u4e00\u4e2a volatile \u53d8\u91cf\uff0c\u90a3\u4e48\u7ebf\u7a0b A \u5728<strong>\u5199\u5165<\/strong> volatile \u53d8\u91cf\u4e4b\u524d\u5bf9\u7ebf\u7a0b A \u53ef\u89c1\u7684\u6240\u6709\u53d8\u91cf\uff0c\u5728\u7ebf\u7a0b B <strong>\u8bfb\u53d6<\/strong> volatile \u53d8\u91cf\u4e4b\u540e\u4e5f\u5bf9\u7ebf\u7a0b B \u53ef\u89c1\u3002<\/li>\n\n\n\n<li><strong>\u3010\u53ef\u89c1\u6027\u5b9a\u7406\u4e8c\u3011<\/strong>\u5982\u679c\u7ebf\u7a0b A <strong>\u8bfb\u53d6<\/strong>\u4e00\u4e2a volatile \u53d8\u91cf\uff0c\u90a3\u4e48\u5728\u8bfb\u53d6\u8be5 volatile \u53d8\u91cf\u65f6\u7ebf\u7a0b A \u53ef\u89c1\u7684\u6240\u6709\u53d8\u91cf\uff0c\u4e5f\u5c06\u4ece\u4e3b\u5b58\u50a8\u5668\u4e2d<strong>\u91cd\u65b0<\/strong>\u8bfb\u53d6\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u4e0b\u9762\u4e3e\u4e24\u4e2a\u4f8b\u5b50\u6765\u7406\u89e3\u4e0a\u9762\u4e24\u70b9\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class TestObj {\n     private int num1;\n     private int num2;\n     private volatile int num3; \/\/ \u53ea\u6709 num3 \u8fd9\u4e2a\u53d8\u91cf\u88ab volatile \u4fee\u9970\u4e86\n     public void update(int num1, int num2, int num3){\n         this.num1  = num1;\n         this.num2 = num2;\n         this.num3   = num3;\n     }\n }<\/code><\/pre>\n\n\n\n<p>\u5982\u4e0a\uff0c\u53ea\u6709 num3 \u8fd9\u4e2a\u53d8\u91cf\u88ab volatile \u4fee\u9970\u4e86\uff08num3 \u8fd9\u4e2a\u53d8\u91cf\u4f1a\u88ab\u6807\u8bb0\u4e3a\u5b58\u50a8\u5728\u4e3b\u5b58\u4e2d\uff09\uff0c\u7136\u540e\u5728 update() \u65b9\u6cd5\u4e2d\u5bf9 num3 \u8fdb\u884c\u4e86\u5199\u5165\u64cd\u4f5c\uff0c\u6839\u636e<strong>\u3010\u53ef\u89c1\u6027\u5b9a\u7406\u4e00\u3011<\/strong>\u5bf9\u4e8e\u7ebf\u7a0b\u53ef\u89c1\u7684\u53d8\u91cf num1 \u548c num2 \u4e5f\u540c\u6837\u4f1a\u88ab\u6807\u8bb0\u4e3a\u5b58\u50a8\u5728\u4e3b\u5b58\u4e2d\uff0c\u6240\u4ee5\u5bf9\u53d8\u91cf num1\u3001num2\u3001num3 \u7684\u8bfb\u5199\u90fd\u662f\u57fa\u4e8e\u4e3b\u5b58\u8fdb\u884c\u7684\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u6307\u4ee4\u91cd\u6392\u5e8f<\/h3>\n\n\n\n<p>volatile \u8fd8\u53ef\u4ee5\u7981\u6b62\u5bf9\u5176\u4fee\u9970\u7684\u53d8\u91cf\u8fdb\u884c\u6307\u4ee4\u91cd\u6392\u5e8f\uff0c\u5173\u4e8e\u6307\u4ee4\u91cd\u6392\u5e8f\u89c1\u6587\u7ae0\uff1a<a href=\"http:\/\/codermr.com\/index.php\/2022\/11\/05\/about_memory_reordering\/\" data-type=\"URL\" data-id=\"http:\/\/codermr.com\/index.php\/2022\/11\/05\/about_memory_reordering\/\" target=\"_blank\" rel=\"noreferrer noopener\">\u5173\u4e8e\u6307\u4ee4\u91cd\u6392\u5e8f<\/a>\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u5173\u4e8e i++<\/h1>\n\n\n\n<p>i++ \u5e76\u4e0d\u662f\u4e00\u4e2a\u539f\u5b50\u64cd\u4f5c\uff0c\u5b83\u7684\u6267\u884c\u8fc7\u7a0b\u5982\u4e0b\uff1a<\/p>\n\n\n\n<ul>\n<li>\u4ece\u4e3b\u5b58\u4e2d\u8bfb\u53d6\u53d8\u91cf i \u526f\u672c\u5230\u7ebf\u7a0b\u7684\u5de5\u4f5c\u5185\u5b58<\/li>\n\n\n\n<li>\u7ed9 i \u52a0 1<\/li>\n\n\n\n<li>\u5c06 i \u52a0 1 \u540e\u7684\u503c\u5199\u56de\u4e3b\u5b58<\/li>\n<\/ul>\n\n\n\n<h1 class=\"wp-block-heading\">\u5b8c\u3002<\/h1>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\uff0c\u6211\u4f1a\u4ece\u300e\u7f13\u5b58\u300f\u3001\u300eCPU \u9ad8\u901f\u7f13\u5b58\u300f\u8bf4\u8d77\uff0c\u7136\u540e\u7ed3\u5408 JMM \u6765\u5e2e\u52a9\u5927\u5bb6\u9010\u6b65\u5b66\u4e60\u548c\u7406\u89e3 volatile &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[11],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>volatile \u5173\u952e\u5b57\u8be6\u89e3 - \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\/03\/whatis_volatile\/\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"volatile \u5173\u952e\u5b57\u8be6\u89e3 - \u7801\u5148\u751f\u7684\u535a\u5ba2\" \/>\n<meta property=\"og:description\" content=\"\u672c\u6587\uff0c\u6211\u4f1a\u4ece\u300e\u7f13\u5b58\u300f\u3001\u300eCPU \u9ad8\u901f\u7f13\u5b58\u300f\u8bf4\u8d77\uff0c\u7136\u540e\u7ed3\u5408 JMM \u6765\u5e2e\u52a9\u5927\u5bb6\u9010\u6b65\u5b66\u4e60\u548c\u7406\u89e3 volatile ...\" \/>\n<meta property=\"og:url\" content=\"http:\/\/codermr.com\/index.php\/2022\/11\/03\/whatis_volatile\/\" \/>\n<meta property=\"og:site_name\" content=\"\u7801\u5148\u751f\u7684\u535a\u5ba2\" \/>\n<meta property=\"article:published_time\" content=\"2022-11-03T15:17:18+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-02-18T01:13:37+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/codermr.com\/wp-content\/uploads\/2023\/01\/image-4-1024x444-1.png\" \/>\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\/03\/whatis_volatile\/#article\",\"isPartOf\":{\"@id\":\"http:\/\/codermr.com\/index.php\/2022\/11\/03\/whatis_volatile\/\"},\"author\":{\"name\":\"\u7801\u5148\u751f\",\"@id\":\"http:\/\/codermr.com\/#\/schema\/person\/39016e15c79e4f02d1ed3a64688619bf\"},\"headline\":\"volatile \u5173\u952e\u5b57\u8be6\u89e3\",\"datePublished\":\"2022-11-03T15:17:18+00:00\",\"dateModified\":\"2023-02-18T01:13:37+00:00\",\"mainEntityOfPage\":{\"@id\":\"http:\/\/codermr.com\/index.php\/2022\/11\/03\/whatis_volatile\/\"},\"wordCount\":96,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/codermr.com\/#\/schema\/person\/39016e15c79e4f02d1ed3a64688619bf\"},\"keywords\":[\"\u7ebf\u7a0b\u5b89\u5168\"],\"articleSection\":[\"JAVA\"],\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"http:\/\/codermr.com\/index.php\/2022\/11\/03\/whatis_volatile\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"http:\/\/codermr.com\/index.php\/2022\/11\/03\/whatis_volatile\/\",\"url\":\"http:\/\/codermr.com\/index.php\/2022\/11\/03\/whatis_volatile\/\",\"name\":\"volatile \u5173\u952e\u5b57\u8be6\u89e3 - \u7801\u5148\u751f\u7684\u535a\u5ba2\",\"isPartOf\":{\"@id\":\"http:\/\/codermr.com\/#website\"},\"datePublished\":\"2022-11-03T15:17:18+00:00\",\"dateModified\":\"2023-02-18T01:13:37+00:00\",\"breadcrumb\":{\"@id\":\"http:\/\/codermr.com\/index.php\/2022\/11\/03\/whatis_volatile\/#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/codermr.com\/index.php\/2022\/11\/03\/whatis_volatile\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/codermr.com\/index.php\/2022\/11\/03\/whatis_volatile\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"http:\/\/codermr.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"volatile \u5173\u952e\u5b57\u8be6\u89e3\"}]},{\"@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":"volatile \u5173\u952e\u5b57\u8be6\u89e3 - \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\/03\/whatis_volatile\/","og_locale":"zh_CN","og_type":"article","og_title":"volatile \u5173\u952e\u5b57\u8be6\u89e3 - \u7801\u5148\u751f\u7684\u535a\u5ba2","og_description":"\u672c\u6587\uff0c\u6211\u4f1a\u4ece\u300e\u7f13\u5b58\u300f\u3001\u300eCPU \u9ad8\u901f\u7f13\u5b58\u300f\u8bf4\u8d77\uff0c\u7136\u540e\u7ed3\u5408 JMM \u6765\u5e2e\u52a9\u5927\u5bb6\u9010\u6b65\u5b66\u4e60\u548c\u7406\u89e3 volatile ...","og_url":"http:\/\/codermr.com\/index.php\/2022\/11\/03\/whatis_volatile\/","og_site_name":"\u7801\u5148\u751f\u7684\u535a\u5ba2","article_published_time":"2022-11-03T15:17:18+00:00","article_modified_time":"2023-02-18T01:13:37+00:00","og_image":[{"url":"http:\/\/codermr.com\/wp-content\/uploads\/2023\/01\/image-4-1024x444-1.png"}],"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\/03\/whatis_volatile\/#article","isPartOf":{"@id":"http:\/\/codermr.com\/index.php\/2022\/11\/03\/whatis_volatile\/"},"author":{"name":"\u7801\u5148\u751f","@id":"http:\/\/codermr.com\/#\/schema\/person\/39016e15c79e4f02d1ed3a64688619bf"},"headline":"volatile \u5173\u952e\u5b57\u8be6\u89e3","datePublished":"2022-11-03T15:17:18+00:00","dateModified":"2023-02-18T01:13:37+00:00","mainEntityOfPage":{"@id":"http:\/\/codermr.com\/index.php\/2022\/11\/03\/whatis_volatile\/"},"wordCount":96,"commentCount":0,"publisher":{"@id":"http:\/\/codermr.com\/#\/schema\/person\/39016e15c79e4f02d1ed3a64688619bf"},"keywords":["\u7ebf\u7a0b\u5b89\u5168"],"articleSection":["JAVA"],"inLanguage":"zh-Hans","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["http:\/\/codermr.com\/index.php\/2022\/11\/03\/whatis_volatile\/#respond"]}]},{"@type":"WebPage","@id":"http:\/\/codermr.com\/index.php\/2022\/11\/03\/whatis_volatile\/","url":"http:\/\/codermr.com\/index.php\/2022\/11\/03\/whatis_volatile\/","name":"volatile \u5173\u952e\u5b57\u8be6\u89e3 - \u7801\u5148\u751f\u7684\u535a\u5ba2","isPartOf":{"@id":"http:\/\/codermr.com\/#website"},"datePublished":"2022-11-03T15:17:18+00:00","dateModified":"2023-02-18T01:13:37+00:00","breadcrumb":{"@id":"http:\/\/codermr.com\/index.php\/2022\/11\/03\/whatis_volatile\/#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["http:\/\/codermr.com\/index.php\/2022\/11\/03\/whatis_volatile\/"]}]},{"@type":"BreadcrumbList","@id":"http:\/\/codermr.com\/index.php\/2022\/11\/03\/whatis_volatile\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"http:\/\/codermr.com\/"},{"@type":"ListItem","position":2,"name":"volatile \u5173\u952e\u5b57\u8be6\u89e3"}]},{"@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\/11"}],"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=11"}],"version-history":[{"count":15,"href":"http:\/\/codermr.com\/index.php\/wp-json\/wp\/v2\/posts\/11\/revisions"}],"predecessor-version":[{"id":619,"href":"http:\/\/codermr.com\/index.php\/wp-json\/wp\/v2\/posts\/11\/revisions\/619"}],"wp:attachment":[{"href":"http:\/\/codermr.com\/index.php\/wp-json\/wp\/v2\/media?parent=11"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/codermr.com\/index.php\/wp-json\/wp\/v2\/categories?post=11"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/codermr.com\/index.php\/wp-json\/wp\/v2\/tags?post=11"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}