{"id":4047,"date":"2024-04-29T08:31:03","date_gmt":"2024-04-29T00:31:03","guid":{"rendered":"https:\/\/xinchewhd.com.cn\/index.php\/%e9%9d%a2%e8%af%95\/%e9%9d%a2%e8%af%95%e5%ae%98%ef%bc%9a%e5%8f%af%e4%bb%a5%e9%80%9a%e8%bf%87%e5%a2%9e%e5%8a%a0volatile%e5%85%b3%e9%94%ae%e5%ad%97%e6%9d%a5%e8%a7%a3%e5%86%b3double-checked-locking%e9%97%ae%e9%a2%98\/"},"modified":"2024-04-29T08:31:03","modified_gmt":"2024-04-29T00:31:03","slug":"%e9%9d%a2%e8%af%95%e5%ae%98%ef%bc%9a%e5%8f%af%e4%bb%a5%e9%80%9a%e8%bf%87%e5%a2%9e%e5%8a%a0volatile%e5%85%b3%e9%94%ae%e5%ad%97%e6%9d%a5%e8%a7%a3%e5%86%b3double-checked-locking%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"https:\/\/xinchewhd.com.cn\/index.php\/%e9%9d%a2%e8%af%95\/%e9%9d%a2%e8%af%95%e5%ae%98%ef%bc%9a%e5%8f%af%e4%bb%a5%e9%80%9a%e8%bf%87%e5%a2%9e%e5%8a%a0volatile%e5%85%b3%e9%94%ae%e5%ad%97%e6%9d%a5%e8%a7%a3%e5%86%b3double-checked-locking%e9%97%ae%e9%a2%98\/","title":{"rendered":"\u9762\u8bd5\u5b98\uff1a\u53ef\u4ee5\u901a\u8fc7\u589e\u52a0volatile\u5173\u952e\u5b57\u6765\u89e3\u51b3Double Checked Locking\u95ee\u9898\u5417\uff1f"},"content":{"rendered":"<div class='wxsyncmain'>\n<section style=\"margin-bottom: 8px;\"><span style=\"font-size: 15px;\">Double Checked Locking\uff08\u53cc\u91cd\u68c0\u67e5\u9501\u5b9a\uff09\u662f\u4e00\u79cd\u591a\u7ebf\u7a0b\u8bbe\u8ba1\u6a21\u5f0f\uff0c\u7528\u4ee5\u51cf\u5c11\u540c\u6b65\u7684\u5f00\u9500\u3002\u8fd9\u4e2a\u6a21\u5f0f\u5728\u5355\u4f8b\u6a21\u5f0f\u4e2d\u7ecf\u5e38\u88ab\u4f7f\u7528\uff0c\u65e8\u5728\u53ea\u540c\u6b65\u4e00\u6b21\u5b9e\u4f8b\u7684\u521b\u5efa\u8fc7\u7a0b\u3002\u4f46\u5728Java\u4e2d\uff0c\u7531\u4e8e\u5185\u5b58\u6a21\u578b\u7684\u539f\u56e0\uff0c\u8fd9\u4e2a\u6a21\u5f0f\u5e76\u4e0d\u603b\u80fd\u6b63\u5e38\u5de5\u4f5c\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5bf9\u8c61\u5904\u4e8e\u4e0d\u4e00\u81f4\u7684\u72b6\u6001\u3002<\/span><\/section>\n<section style=\"margin-bottom: 8px;\"><span style=\"font-size: 15px;\">volatile\u5173\u952e\u5b57\u786e\u5b9e\u53ef\u4ee5\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u89e3\u51b3Double Checked Locking\u95ee\u9898\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684\u540c\u6b65\u673a\u5236\uff0c\u786e\u4fdd\u7ebf\u7a0b\u5bf9\u53d8\u91cf\u7684\u5199\u5165\u64cd\u4f5c\u5bf9\u5176\u4ed6\u7ebf\u7a0b\u53ef\u89c1\uff0c\u5e76\u4e14\u4e0d\u4f1a\u53d1\u751f\u6307\u4ee4\u91cd\u6392\u5e8f\uff0c\u8fd9\u662f\u5b9e\u73b0\u53cc\u91cd\u68c0\u67e5\u9501\u5b9a\u7684\u6b63\u786e\u6027\u6240\u5fc5\u9700\u7684\u3002<\/span><\/section>\n<p data-v-md-line=\"5\"><span style=\"font-size: 15px;\">\u5728Java 5\u53ca\u4ee5\u4e0a\u7248\u672c\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u5c06\u5355\u4f8b\u7684\u5b9e\u4f8b\u58f0\u660e\u4e3avolatile\u6765\u4fee\u590d\u53cc\u91cd\u68c0\u67e5\u9501\u5b9a\uff08\u5982\u679c\u4f60\u7684\u4ee3\u7801\u8fd0\u884c\u5728\u6b64\u4e4b\u524d\u7684\u7248\u672c\uff0c\u8bf7\u6ce8\u610f\u8fd9\u79cd\u4fee\u590d\u65b9\u6cd5\u5e76\u4e0d\u9002\u7528\uff09\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528volatile\u5173\u952e\u5b57\u7684Double Checked Locking\u5355\u4f8b\u6a21\u5f0f\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<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<li><\/li>\n<li><\/li>\n<\/ul>\n<pre class=\"code-snippet__js\" data-lang=\"java\"><code><span class=\"code-snippet_outer\"><span class=\"code-snippet__keyword\">public<\/span> <span class=\"code-snippet__class\"><span class=\"code-snippet__keyword\">class<\/span> <span class=\"code-snippet__title\">Singleton<\/span> <\/span>{<\/span><\/code><code><span class=\"code-snippet_outer\"><\/span><\/code><code><span class=\"code-snippet_outer\">    <span class=\"code-snippet__comment\">\/\/ \u4f7f\u7528volatile\u5173\u952e\u5b57\u4fee\u9970\u5355\u4f8b\u5b9e\u4f8b<\/span><\/span><\/code><code><span class=\"code-snippet_outer\">    <span class=\"code-snippet__keyword\">private<\/span> <span class=\"code-snippet__keyword\">static<\/span> <span class=\"code-snippet__keyword\">volatile<\/span> Singleton instance;<\/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\">private<\/span> <span class=\"code-snippet__title\">Singleton<\/span><span class=\"code-snippet__params\">()<\/span> <\/span>{<\/span><\/code><code><span class=\"code-snippet_outer\">        <span class=\"code-snippet__comment\">\/\/ \u6784\u9020\u51fd\u6570\u79c1\u6709\u5316<\/span><\/span><\/code><code><span class=\"code-snippet_outer\">    }<\/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\">static<\/span> Singleton <span class=\"code-snippet__title\">getInstance<\/span><span class=\"code-snippet__params\">()<\/span> <\/span>{<\/span><\/code><code><span class=\"code-snippet_outer\">        <span class=\"code-snippet__keyword\">if<\/span> (instance == <span class=\"code-snippet__keyword\">null<\/span>) { <span class=\"code-snippet__comment\">\/\/ \u7b2c\u4e00\u6b21\u68c0\u67e5<\/span><\/span><\/code><code><span class=\"code-snippet_outer\">            <span class=\"code-snippet__keyword\">synchronized<\/span> (Singleton.class) { <span class=\"code-snippet__comment\">\/\/ \u540c\u6b65\u5757<\/span><\/span><\/code><code><span class=\"code-snippet_outer\">                <span class=\"code-snippet__keyword\">if<\/span> (instance == <span class=\"code-snippet__keyword\">null<\/span>) { <span class=\"code-snippet__comment\">\/\/ \u7b2c\u4e8c\u6b21\u68c0\u67e5<\/span><\/span><\/code><code><span class=\"code-snippet_outer\">                    instance = <span class=\"code-snippet__keyword\">new<\/span> Singleton(); <span class=\"code-snippet__comment\">\/\/ \u6ce8\u610f\uff0c\u6b64\u65f6volatile\u7981\u6b62\u6307\u4ee4\u91cd\u6392\u5e8f<\/span><\/span><\/code><code><span class=\"code-snippet_outer\">                }<\/span><\/code><code><span class=\"code-snippet_outer\">            }<\/span><\/code><code><span class=\"code-snippet_outer\">        }<\/span><\/code><code><span class=\"code-snippet_outer\">        <span class=\"code-snippet__keyword\">return<\/span> instance;<\/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=\"30\" style=\"margin-bottom: 8px;\"><span style=\"font-size: 15px;\">\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0cinstance\u53d8\u91cf\u88ab\u58f0\u660e\u4e3avolatile\uff0c\u786e\u4fdd\u4e86\u4ee5\u4e0b\u4e24\u70b9\uff1a<\/span><\/p>\n<ol data-v-md-line=\"32\" style=\"margin-bottom: 8px;\" class=\"list-paddingleft-1\">\n<li style=\"margin-bottom: 8px;\">\n<p style=\"margin-bottom: 8px;\"><span style=\"font-size: 15px;\">\u53ef\u89c1\u6027\uff1a\u5f53instance\u53d8\u91cf\u88ab\u521d\u59cb\u5316\u6210Singleton\u5b9e\u4f8b\u65f6\uff0c\u591a\u4e2a\u7ebf\u7a0b\u6b63\u786e\u5730\u5904\u7406instance\u53d8\u91cf\u7684\u72b6\u6001\u3002<\/span><\/p>\n<\/li>\n<li style=\"margin-bottom: 8px;\">\n<p style=\"margin-bottom: 8px;\"><span style=\"font-size: 15px;\"><\/span><span style=\"font-size: 15px;\">\u9632\u6b62\u6307\u4ee4\u91cd\u6392\u5e8f\uff1a\u5728\u6ca1\u6709volatile\u4fee\u9970\u7b26\u7684\u60c5\u51b5\u4e0b\uff0c\u7f16\u8bd1\u5668\u53ef\u80fd\u4f1a\u91cd\u65b0\u6392\u5e8f\u6307\u4ee4\uff0c\u5e76\u4e14\u5176\u4ed6\u7ebf\u7a0b\u53ef\u80fd\u770b\u5230\u4e00\u4e2a\u8fd8\u6ca1\u6709\u5b8c\u5168\u6784\u5efa\u7684\u5b9e\u4f8b\uff08\u5373\u534a\u521d\u59cb\u5316\u7684\u5b9e\u4f8b\uff09\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u7a0b\u5e8f\u51fa\u73b0\u9519\u8bef\u7684\u884c\u4e3a\u3002<\/span><\/p>\n<\/li>\n<\/ol>\n<p style=\"margin-bottom: 8px;\"><strong><span style=\"font-size: 15px;\">\u6ce8\u610f<\/span><span style=\"font-size: 15px;\">\uff1a<\/span><\/strong><span style=\"font-size: 15px;\">instance = new Singleton()\u8fd9\u6761\u8bed\u53e5\u5b9e\u9645\u4e0a\u53ef\u4ee5\u5206\u89e3\u4e3a\u4ee5\u4e0b\u51e0\u4e2a\u6307\u4ee4\uff1a<\/span><\/p>\n<ol class=\"list-paddingleft-1\" style=\"margin-bottom: 8px;\">\n<li style=\"margin-bottom: 8px;\">\n<p style=\"margin-bottom: 8px;\"><strong><span style=\"font-size: 15px;\">\u5206\u914d\u5185\u5b58<\/span><\/strong><span style=\"font-size: 15px;\">\uff1a\u4e3a Sing<\/span><span style=\"font-size: 15px;\">leton \u5bf9\u8c61\u5206\u914d\u5185\u5b58\u7a7a\u95f4\u3002<\/span><\/p>\n<\/li>\n<li style=\"margin-bottom: 8px;\">\n<p style=\"margin-bottom: 8px;\"><strong><span style=\"font-size: 15px;\">\u521d\u59cb\u5316\u5bf9\u8c61<\/span><\/strong><span style=\"font-size: 15px;\">\uff1a\u8c03\u7528 Singleton \u7684\u6784\u9020\u51fd\u6570\uff0c\u521d\u59cb\u5316\u5bf9\u8c61\u3002<\/span><\/p>\n<\/li>\n<li style=\"margin-bottom: 8px;\">\n<p style=\"margin-bottom: 8px;\"><strong><span style=\"font-size: 15px;\">\u5c06\u5f15\u7528\u6307\u5411\u5206\u914d\u7684\u5185\u5b58<\/span><\/strong><span style=\"font-size: 15px;\">\uff1a\u5c06 instance \u5f15\u7528\u6307\u5411\u65b0\u521b\u5efa\u7684 Singleton \u5bf9\u8c61\u3002<\/span><\/p>\n<\/li>\n<\/ol>\n<section style=\"margin-bottom: 24px;\"><span style=\"font-size: 15px;\">\u5982\u679c\u6ca1\u6709volatile\u4fee\u9970\u7b26\uff0c\u7531\u4e8e\u7f16\u8bd1\u5668\u4f18\u5316\u90a3\u4e48\u53ef\u80fd\u51fa\u73b0<strong>1-&gt;3-&gt;2<\/strong>\u7684\u6267\u884c\u987a\u5e8f\u3002<\/span><span style=\"font-size: 15px;\"><\/span><\/section>\n<p data-v-md-line=\"35\" style=\"margin-bottom: 8px;\"><strong><span style=\"font-size: 20px;\">\u7ed3\u8bba<\/span><\/strong><\/p>\n<p data-v-md-line=\"35\" style=\"margin-bottom: 8px;\"><span style=\"font-size: 15px;\">\u901a\u8fc7\u5728\u58f0\u660e\u5b57\u6bb5\u65f6\u4f7f\u7528volatile\u5173\u952e\u5b57\uff0c\u53ef\u4ee5\u89e3\u51b3Double Checked Locking\u95ee\u9898\uff0c\u786e\u4fdd\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e2d\u5171\u4eab\u53d8\u91cf\u7684\u72b6\u6001\u5bf9\u4e8e\u6240\u6709\u7ebf\u7a0b\u90fd\u662f\u4e00\u81f4\u7684\u3002\u7136\u800c\uff0c\u5373\u4f7f\u4f7f\u7528\u4e86volatile\uff0cDouble Checked Locking\u4f9d\u7136\u662f\u4e00\u4e2a\u5bb9\u6613\u51fa\u9519\u7684\u6a21\u5f0f\uff0c\u56e0\u6b64\u901a\u5e38\u5efa\u8bae\u4f7f\u7528\u5176\u4ed6\u66f4\u7b80\u5355\u3001\u66f4\u6e05\u6670\u7684\u65b9\u5f0f\u6765\u5b9e\u73b0\u7ebf\u7a0b\u5b89\u5168\u7684\u5355\u4f8b\uff0c\u4f8b\u5982\u4f7f\u7528\u679a\u4e3e\u7c7b\u578b\u6216\u8005\u5185\u90e8\u9759\u6001\u7c7b\u6765\u5b9e\u73b0\u5355\u4f8b\u6a21\u5f0f\u3002<\/span><span style=\"font-size: 15px;letter-spacing: 0.034em;\"><\/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>Double Checked Locking\uff08\u53cc\u91cd\u68c0\u67e5\u9501\u5b9a\uff09\u662f\u4e00\u79cd\u591a\u7ebf\u7a0b\u8bbe\u8ba1\u6a21\u5f0f\uff0c\u7528\u4ee5\u51cf\u5c11\u540c\u6b65\u7684\u5f00\u9500\u3002\u8fd9\u4e2a\u6a21\u5f0f\u5728\u5355\u4f8b\u6a21\u5f0f\u4e2d\u7ecf\u5e38\u88ab\u4f7f &#8230;<\/p>\n","protected":false},"author":1,"featured_media":4044,"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-4047","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\/20240524144720820-150x150.jpeg","content_first_image":null,"post_medium_image_300":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524144720820-300x128.jpeg","post_thumbnail_image_624":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524144720820-1024x435.jpeg","post_frist_image":null,"post_medium_image":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524144720820-300x128.jpeg","post_large_image":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524144720820-1024x435.jpeg","post_full_image":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524144720820.jpeg","post_all_images":[],"videoAdId":"","listAd":"0","listAdId":"","listAdEvery":6,"total_comments":0,"category_name":"\u9762\u8bd5","post_date":"2024-04-29","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":404,"next_post_id":4505,"next_post_title":"\u8682\u8681\u9762\u8bd5\uff1aSpringcloud\u6838\u5fc3\u7ec4\u4ef6\u7684\u5e95\u5c42\u539f\u7406\uff0c\u4f60\u77e5\u9053\u591a\u5c11\uff1f","previous_post_id":4051,"previous_post_title":"\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","_links":{"self":[{"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/posts\/4047","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=4047"}],"version-history":[{"count":0,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/posts\/4047\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/media\/4044"}],"wp:attachment":[{"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/media?parent=4047"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/categories?post=4047"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/tags?post=4047"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}