{"id":4051,"date":"2024-04-28T08:31:00","date_gmt":"2024-04-28T00:31:00","guid":{"rendered":"https:\/\/xinchewhd.com.cn\/index.php\/%e9%9d%a2%e8%af%95\/%e8%85%be%e8%ae%af%e4%b8%80%e9%9d%a2%ef%bc%9a%e4%b8%ba%e4%bb%80%e4%b9%88volatile%e4%b8%8d%e8%83%bd%e4%bf%9d%e8%af%81%e5%a4%8d%e5%90%88%e6%93%8d%e4%bd%9c%ef%bc%88%e5%a6%82%e8%87%aa%e5%a2%9e%e6%93%8d\/"},"modified":"2024-04-28T08:31:00","modified_gmt":"2024-04-28T00:31:00","slug":"%e8%85%be%e8%ae%af%e4%b8%80%e9%9d%a2%ef%bc%9a%e4%b8%ba%e4%bb%80%e4%b9%88volatile%e4%b8%8d%e8%83%bd%e4%bf%9d%e8%af%81%e5%a4%8d%e5%90%88%e6%93%8d%e4%bd%9c%ef%bc%88%e5%a6%82%e8%87%aa%e5%a2%9e%e6%93%8d","status":"publish","type":"post","link":"https:\/\/xinchewhd.com.cn\/index.php\/%e9%9d%a2%e8%af%95\/%e8%85%be%e8%ae%af%e4%b8%80%e9%9d%a2%ef%bc%9a%e4%b8%ba%e4%bb%80%e4%b9%88volatile%e4%b8%8d%e8%83%bd%e4%bf%9d%e8%af%81%e5%a4%8d%e5%90%88%e6%93%8d%e4%bd%9c%ef%bc%88%e5%a6%82%e8%87%aa%e5%a2%9e%e6%93%8d\/","title":{"rendered":"\u817e\u8baf\u4e00\u9762\uff1a\u4e3a\u4ec0\u4e48volatile\u4e0d\u80fd\u4fdd\u8bc1\u590d\u5408\u64cd\u4f5c\uff08\u5982\u81ea\u589e\u64cd\u4f5c i++\uff09\u7684\u7ebf\u7a0b\u5b89\u5168\uff1f"},"content":{"rendered":"<div class='wxsyncmain'>\n<p data-v-md-line=\"1\"><strong><span style=\"font-size: 15px;\">volatile <\/span><\/strong><span style=\"font-size: 15px;\">\u5173\u952e\u5b57\u4e0d\u80fd\u4fdd\u8bc1\u590d\u5408\u64cd\u4f5c\uff08\u5982\u81ea\u589e\u64cd\u4f5c i++\uff09\u7684\u7ebf\u7a0b\u5b89\u5168\uff0c\u56e0\u4e3a\u590d\u5408\u64cd\u4f5c\u4e0d\u662f\u5355\u4e00\u7684\uff0c\u800c\u662f\u7531\u591a\u4e2a\u72ec\u7acb\u7684\u6b65\u9aa4\u7ec4\u6210\u7684\uff0c\u8fd9\u4e9b\u6b65\u9aa4\u53ef\u80fd\u4f1a\u4ea4\u9519\u6267\u884c\u3002\u81ea\u589e\u64cd\u4f5c\u53ef\u4ee5\u5206\u89e3\u4e3a\u4ee5\u4e0b\u4e09\u4e2a\u57fa\u672c\u6b65\u9aa4\uff1a<\/span><\/p>\n<ol data-v-md-line=\"3\" class=\"list-paddingleft-1\" style=\"\">\n<li>\n<p><strong><span style=\"font-size: 15px;\">\u8bfb\u53d6<\/span><\/strong><span style=\"font-size: 15px;\">\uff1a\u5c06\u53d8\u91cf i \u7684\u5f53\u524d\u503c\u8bfb\u5165\u5bc4\u5b58\u5668\u6216\u5176\u4ed6\u4e34\u65f6\u5185\u5b58\u4f4d\u7f6e\u3002<\/span><\/p>\n<\/li>\n<li>\n<p><strong><span style=\"font-size: 15px;\">\u4fee\u6539<\/span><\/strong><span style=\"font-size: 15px;\">\uff1a\u5728\u5bc4\u5b58\u5668\u4e2d\u5c06\u4e34\u65f6\u503c\u589e\u52a01\u3002<\/span><\/p>\n<\/li>\n<li>\n<p><strong><span style=\"font-size: 15px;\">\u5199\u5165<\/span><\/strong><span style=\"font-size: 15px;\">\uff1a\u5c06\u65b0\u503c\u5199\u56de\u4e3b\u5185\u5b58\u4e2d\u7684\u53d8\u91cf i\u3002<\/span><\/p>\n<\/li>\n<\/ol>\n<section style=\"margin-bottom: 16px;\"><span style=\"font-size: 15px;\">\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u58f0\u660e\u4e3a volatile \u7684\u6574\u6570 i\uff1a<\/span><\/section>\n<section class=\"code-snippet__fix code-snippet__js\">\n<ul class=\"code-snippet__line-index code-snippet__js\">\n<li><\/li>\n<\/ul>\n<pre class=\"code-snippet__js\" data-lang=\"cs\"><code><span class=\"code-snippet_outer\"><span class=\"code-snippet__keyword\">volatile<\/span> <span class=\"code-snippet__keyword\">int<\/span> i = <span class=\"code-snippet__number\">0<\/span>;<\/span><\/code><\/pre>\n<\/section>\n<p data-v-md-line=\"13\"><span style=\"font-size: 15px;\">\u73b0\u5728\u8003\u8651\u4ee5\u4e0b\u4e24\u4e2a\u7ebf\u7a0b\u540c\u65f6\u8fd0\u884c\u4ee3\u7801 i++;\u3002\u6211\u4eec\u5206\u89e3\u6bcf\u4e2a\u6b65\u9aa4\u6765\u770b\u53d1\u751f\u4e86\u4ec0\u4e48\uff1a<\/span><\/p>\n<table data-v-md-line=\"15\">\n<thead style=\"\">\n<tr style=\"\">\n<th style=\"\"><span style=\"font-size: 15px;\">\u65f6\u95f4\u70b9<\/span><\/th>\n<th style=\"\"><span style=\"font-size: 15px;\">\u7ebf\u7a0b A<\/span><\/th>\n<th style=\"\"><span style=\"font-size: 15px;\">\u7ebf\u7a0b B<\/span><\/th>\n<\/tr>\n<\/thead>\n<tbody style=\"\">\n<tr style=\"\">\n<td style=\"\"><span style=\"font-size: 15px;\">T1<\/span><\/td>\n<td style=\"\"><span style=\"font-size: 15px;\">\u8bfb\u53d6i\u7684\u503c (0)\u5230\u5bc4\u5b58\u5668A<\/span><\/td>\n<td style=\"\"><\/td>\n<\/tr>\n<tr style=\"\">\n<td style=\"\"><span style=\"font-size: 15px;\">T2<\/span><\/td>\n<td style=\"\"><\/td>\n<td style=\"\"><span style=\"font-size: 15px;\">\u8bfb\u53d6i\u7684\u503c (0)\u5230\u5bc4\u5b58\u5668B<\/span><\/td>\n<\/tr>\n<tr style=\"\">\n<td style=\"\"><span style=\"font-size: 15px;\">T3<\/span><\/td>\n<td style=\"\"><span style=\"font-size: 15px;\">\u5c06\u5bc4\u5b58\u5668A\u7684\u503c\u589e\u52a01 (\u5f97\u52301)<\/span><\/td>\n<td style=\"\"><\/td>\n<\/tr>\n<tr style=\"\">\n<td style=\"\"><span style=\"font-size: 15px;\">T4<\/span><\/td>\n<td style=\"\"><\/td>\n<td style=\"\"><span style=\"font-size: 15px;\">\u5c06\u5bc4\u5b58\u5668B\u7684\u503c\u589e\u52a01 (\u5f97\u52301)<\/span><\/td>\n<\/tr>\n<tr style=\"\">\n<td style=\"\"><span style=\"font-size: 15px;\">T5<\/span><\/td>\n<td style=\"\"><span style=\"font-size: 15px;\">\u5c06\u5bc4\u5b58\u5668A\u7684\u503c (1)\u5199\u56dei<\/span><\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-size: 15px;\">T6<\/span><\/td>\n<td><\/td>\n<td><span style=\"font-size: 15px;\">\u5c06\u5bc4\u5b58\u5668B\u7684\u503c (1)\u5199\u56dei<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p data-v-md-line=\"24\"><span style=\"font-size: 15px;\">\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5c3d\u7ba1 i \u88ab\u58f0\u660e\u4e3a volatile \u5e76\u4e14\u6bcf\u4e2a\u8bfb\u53d6\u548c\u5199\u5165\u64cd\u4f5c\u90fd\u662f\u76f4\u63a5\u5bf9\u4e3b\u5185\u5b58\u8fdb\u884c\u7684\uff0c<strong>\u4f46\u6ca1\u6709\u4efb\u4f55\u4e1c\u897f\u9632\u6b62\u4e24\u4e2a\u7ebf\u7a0b\u540c\u65f6\u8bfb\u53d6\u76f8\u540c\u7684\u521d\u59cb\u503c<\/strong>\uff0c\u7136\u540e\u5404\u81ea\u8ba1\u7b97\u51fa\u76f8\u540c\u7684\u589e\u52a0\u540e\u7684\u503c\uff0c\u5e76\u6700\u7ec8\u628a\u8fd9\u4e2a\u76f8\u540c\u7684\u503c\u5199\u56de i\u3002\u5b9e\u9645\u4e0a\uff0c\u6211\u4eec\u671f\u671b i \u7684\u503c\u5728\u4e24\u6b21\u9012\u589e\u540e\u662f2\uff0c\u4f46\u7531\u4e8e\u7ebf\u7a0b\u95f4\u7684\u5e72\u6270\uff0c\u7ed3\u679c\u53ea\u662f\u9012\u589e\u4e86\u4e00\u6b21\uff0c<strong>\u6700\u7ec8\u7684\u503c\u662f1<\/strong>\u3002<\/span><\/p>\n<p data-v-md-line=\"26\"><span style=\"font-size: 15px;\">\u8981\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u5e76<strong>\u786e\u4fdd\u590d\u5408\u64cd\u4f5c\u7684\u539f\u5b50\u6027<\/strong>\uff0c\u5e94\u4f7f\u7528 synchronized \u5173\u952e\u5b57\uff0c\u9501\u673a\u5236\u6216 java.util.concurrent.atomic \u5305\u4e2d\u7684\u539f\u5b50\u7c7b\uff08\u4f8b\u5982 AtomicInteger\uff09\u3002\u4e0b\u9762\u662f\u4f7f\u7528 AtomicInteger \u7684\u793a\u4f8b\uff1a<\/span><\/p>\n<section class=\"code-snippet__fix code-snippet__js\">\n<ul class=\"code-snippet__line-index code-snippet__js\">\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<\/ul>\n<pre class=\"code-snippet__js\" data-lang=\"cpp\"><code><span class=\"code-snippet_outer\"><span class=\"code-snippet__keyword\">import<\/span> java.util.concurrent.atomic.AtomicInteger;<\/span><\/code><code><span class=\"code-snippet_outer\"><\/span><\/code><code><span class=\"code-snippet_outer\"><span class=\"code-snippet__class\"><span class=\"code-snippet__keyword\">class<\/span> <span class=\"code-snippet__title\">Counter<\/span> {<\/span><\/span><\/code><code><span class=\"code-snippet_outer\">    <span class=\"code-snippet__keyword\">private<\/span> AtomicInteger atomicI = <span class=\"code-snippet__keyword\">new<\/span> AtomicInteger(<span class=\"code-snippet__number\">0<\/span>);<\/span><\/code><code><span class=\"code-snippet_outer\"><\/span><\/code><code><span class=\"code-snippet_outer\">    <span class=\"code-snippet__function\"><span class=\"code-snippet__keyword\">public<\/span> <span class=\"code-snippet__keyword\">void<\/span> <span class=\"code-snippet__title\">increment<\/span><span class=\"code-snippet__params\">()<\/span> <\/span>{<\/span><\/code><code><span class=\"code-snippet_outer\">        atomicI.incrementAndGet();<\/span><\/code><code><span class=\"code-snippet_outer\">    }<\/span><\/code><code><span class=\"code-snippet_outer\">}<\/span><\/code><\/pre>\n<\/section>\n<p data-v-md-line=\"7\"><span style=\"font-size: 15px;\">\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0cincrementAndGet()\u65b9\u6cd5\u53ef\u4ee5\u5b89\u5168\u5730\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e2d\u8c03\u7528\uff0c\u65e0\u8bba\u6709\u591a\u5c11\u7ebf\u7a0b\u540c\u65f6\u8c03\u7528\u6b64\u65b9\u6cd5\uff0c\u90fd\u80fd\u591f\u786e\u4fdd\u6bcf\u4e00\u6b21\u8c03\u7528\u90fd\u4f1a\u539f\u5b50\u5730\u5c06 atomicI \u7684\u503c\u589e\u52a01\u3002<\/span><span style=\"font-size: 15px;\"><\/span><\/p>\n<p data-v-md-line=\"7\"><span style=\"font-size: 15px;\">\u8865\u5145\u8bf4\u660e\uff1a\u4e00\u822c\u817e\u8baf\u4e00\u9762\u90fd\u662f\u7279\u522b\u57fa\u7840\u7684\u95ee\u9898\uff0c\u6240\u4ee5\u6211\u4eec\u5fc5\u987b\u52a0\u5f3a\u57fa\u7840\u77e5\u8bc6\u7684\u5b66\u4e60\u3002<\/span><\/p>\n<p style=\"display: none;\"><mp-style-type data-value=\"3\"><\/mp-style-type><\/p>\n<blockquote>\n<p>\u672c\u7bc7\u6587\u7ae0\u6765\u6e90\u4e8e\u5fae\u4fe1\u516c\u4f17\u53f7:                 \u4e92\u8054\u7f51\u9762\u8bd5\u5c0f\u5e2e\u624b              <\/p>\n<\/blockquote>\n<\/div>\n","raw":"","protected":false},"excerpt":{"rendered":"<p>volatile \u5173\u952e\u5b57\u4e0d\u80fd\u4fdd\u8bc1\u590d\u5408\u64cd\u4f5c\uff08\u5982\u81ea\u589e\u64cd\u4f5c i++\uff09\u7684\u7ebf\u7a0b\u5b89\u5168\uff0c\u56e0\u4e3a\u590d\u5408\u64cd\u4f5c\u4e0d\u662f\u5355\u4e00\u7684\uff0c\u800c\u662f\u7531\u591a\u4e2a\u72ec\u7acb\u7684\u6b65\u9aa4\u7ec4\u6210\u7684\uff0c\u8fd9\u4e9b &#8230;<\/p>\n","protected":false},"author":1,"featured_media":4048,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"emotion":"","emotion_color":"","title_style":"","license":"","footnotes":""},"categories":[30],"tags":[],"class_list":["post-4051","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-30"],"post_thumbnail_image":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524144737285-150x150.jpeg","content_first_image":null,"post_medium_image_300":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524144737285-300x128.jpeg","post_thumbnail_image_624":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524144737285-1024x435.jpeg","post_frist_image":null,"post_medium_image":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524144737285-300x128.jpeg","post_large_image":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524144737285-1024x435.jpeg","post_full_image":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524144737285.jpeg","post_all_images":[],"videoAdId":"","listAd":"0","listAdId":"","listAdEvery":6,"total_comments":0,"category_name":"\u9762\u8bd5","post_date":"2024-04-28","like_count":"0","praiseWord":"\u9f13\u52b1","copyright_state":"","excitationAd":"0","rewardedVideoAdId":"","detailAdId":"","detailAd":"0","enterpriseMinapp":"0","audios":[],"postImageUrl":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2023\/05\/20230519082947553.jpg","avatarurls":[],"related_posts":null,"pageviews":333,"next_post_id":4047,"next_post_title":"\u9762\u8bd5\u5b98\uff1a\u53ef\u4ee5\u901a\u8fc7\u589e\u52a0volatile\u5173\u952e\u5b57\u6765\u89e3\u51b3Double Checked Locking\u95ee\u9898\u5417\uff1f","previous_post_id":4055,"previous_post_title":"\u5b57\u8282\u9762\u8bd5\u9898\uff1aJava\u4e2d\u5982\u679c\u4e24\u4e2a\u5b57\u7b26\u4e32\u5bf9\u8c61\u6709\u76f8\u540c\u7684\u503c\uff0c\u5b83\u4eec\u4f1a\u5b58\u50a8\u5728\u5e38\u91cf\u6c60\u4e2d\u7684\u540c\u4e00\u4e2a\u4f4d\u7f6e\u5417\uff1f","_links":{"self":[{"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/posts\/4051","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/comments?post=4051"}],"version-history":[{"count":0,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/posts\/4051\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/media\/4048"}],"wp:attachment":[{"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/media?parent=4051"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/categories?post=4051"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/tags?post=4051"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}