{"id":3972,"date":"2025-07-28T20:46:12","date_gmt":"2025-07-28T12:46:12","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=3972"},"modified":"2025-07-28T20:46:12","modified_gmt":"2025-07-28T12:46:12","slug":"dcl%e5%8d%95%e4%be%8b%e6%a8%a1%e5%bc%8f%e8%ae%be%e8%ae%a1%e4%b8%ba%e4%bb%80%e4%b9%88%e9%9c%80%e8%a6%81volatile%e4%bf%ae%e9%a5%b0%e5%ae%9e%e4%be%8b%e5%af%b9%e8%b1%a1","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/07\/28\/dcl%e5%8d%95%e4%be%8b%e6%a8%a1%e5%bc%8f%e8%ae%be%e8%ae%a1%e4%b8%ba%e4%bb%80%e4%b9%88%e9%9c%80%e8%a6%81volatile%e4%bf%ae%e9%a5%b0%e5%ae%9e%e4%be%8b%e5%af%b9%e8%b1%a1\/","title":{"rendered":"DCL\u5355\u4f8b\u6a21\u5f0f\u8bbe\u8ba1\u4e3a\u4ec0\u4e48\u9700\u8981volatile\u4fee\u9970\u5b9e\u4f8b\u5bf9\u8c61"},"content":{"rendered":"\n<p>DCL\uff08Double-Checked Locking\uff0c\u53cc\u91cd\u68c0\u67e5\u9501\uff09\u5355\u4f8b\u6a21\u5f0f\u4e2d\uff0c<code>volatile<\/code> \u5173\u952e\u5b57\u5bf9\u4e8e\u4fdd\u8bc1 <strong>\u7ebf\u7a0b\u5b89\u5168<\/strong> \u548c <strong>\u6b63\u786e\u7684\u5b9e\u4f8b\u521d\u59cb\u5316<\/strong> \u662f <strong>\u7edd\u5bf9\u5fc5\u8981<\/strong> \u7684\u3002\u6211\u4eec\u6765\u7cfb\u7edf\u5730\u89e3\u6790\u4e3a\u4ec0\u4e48 DCL \u4e2d\u5fc5\u987b\u4f7f\u7528 <code>volatile<\/code> \u4fee\u9970\u5355\u4f8b\u5b9e\u4f8b\u5bf9\u8c61\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e00\u3001DCL \u5355\u4f8b\u6a21\u5f0f\u7684\u7ed3\u6784\uff08\u4e0d\u542b <code>volatile<\/code>\uff09<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>public class Singleton {\n    private static Singleton instance;\n\n    public static Singleton getInstance() {\n        if (instance == null) {  \/\/ \u7b2c\u4e00\u6b21\u68c0\u67e5\n            synchronized (Singleton.class) {\n                if (instance == null) {  \/\/ \u7b2c\u4e8c\u6b21\u68c0\u67e5\n                    instance = new Singleton();\n                }\n            }\n        }\n        return instance;\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u8fd9\u4e2a\u5b9e\u73b0\u76ee\u7684\u662f\u4e3a\u4e86<strong>\u65e2\u907f\u514d\u91cd\u590d\u521b\u5efa\u5b9e\u4f8b<\/strong>\uff0c\u53c8\u80fd\u5728\u7b2c\u4e00\u6b21\u521b\u5efa\u540e\u907f\u514d\u6bcf\u6b21\u8bbf\u95ee\u90fd\u8fdb\u5165 <code>synchronized<\/code>\uff0c\u4ece\u800c\u63d0\u9ad8\u6027\u80fd\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e8c\u3001\u95ee\u9898\uff1a\u4e3a\u4ec0\u4e48\u4e0d\u52a0 <code>volatile<\/code> \u4f1a\u51fa\u9519\uff1f<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. <code>new Singleton()<\/code> \u4e0d\u662f\u539f\u5b50\u64cd\u4f5c<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>instance = new Singleton();<\/code><\/pre>\n\n\n\n<p>\u8fd9\u884c\u8bed\u53e5\u5728 JVM \u4e2d\u4f1a\u88ab\u62c6\u5206\u4e3a <strong>3 \u6b65\u64cd\u4f5c<\/strong>\uff08\u6307\u4ee4\u91cd\u6392\u5e8f\u524d\uff09\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>1\uff09\u5206\u914d\u5185\u5b58\u7ed9\u5bf9\u8c61\n2\uff09\u8c03\u7528\u6784\u9020\u51fd\u6570\u521d\u59cb\u5316\u5bf9\u8c61\n3\uff09\u5c06\u5bf9\u8c61\u5f15\u7528\u8d4b\u503c\u7ed9\u53d8\u91cf\uff08instance \u6307\u5411\u8fd9\u5757\u5185\u5b58\uff09<\/code><\/pre>\n\n\n\n<p>\u4f46\u7531\u4e8e <strong>JVM \u5141\u8bb8\u6307\u4ee4\u91cd\u6392\u5e8f<\/strong>\uff0c\u5b9e\u9645\u6267\u884c\u53ef\u80fd\u53d8\u6210\u8fd9\u6837\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>1\uff09\u5206\u914d\u5185\u5b58\n2\uff09\u5c06\u5bf9\u8c61\u5f15\u7528\u8d4b\u503c\u7ed9\u53d8\u91cf\n3\uff09\u8c03\u7528\u6784\u9020\u51fd\u6570\u521d\u59cb\u5316\u5bf9\u8c61<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2. \u91cd\u6392\u5e8f\u5bfc\u81f4\u7684\u95ee\u9898\uff1a<\/h3>\n\n\n\n<p>\u5982\u679c\u7ebf\u7a0b A \u6267\u884c\u4e86\u91cd\u6392\u5e8f\u5e76\u5728 <code>step 2<\/code> \u8d4b\u503c\u5b8c\u540e\u88ab\u7ebf\u7a0b B \u8bfb\u53d6\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u7ebf\u7a0b B \u53d1\u73b0 <code>instance != null<\/code>\uff0c\u4e8e\u662f\u8df3\u8fc7\u540c\u6b65\u5757\uff0c\u76f4\u63a5\u8fd4\u56de\u3002<\/li>\n\n\n\n<li>\u4f46\u6b64\u65f6 <code>instance<\/code> \u5176\u5b9e\u662f\u4e00\u4e2a <strong>\u5c1a\u672a\u521d\u59cb\u5316\u5b8c\u6210<\/strong> \u7684\u5bf9\u8c61\u3002<\/li>\n\n\n\n<li>\u7ebf\u7a0b B \u8bbf\u95ee\u8be5\u5bf9\u8c61\u65f6\u53ef\u80fd\u5f15\u53d1 <strong>\u7a7a\u6307\u9488\u5f02\u5e38<\/strong> \u6216\u5176\u4ed6\u672a\u5b9a\u4e49\u884c\u4e3a\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e09\u3001volatile \u7684\u4f5c\u7528\uff1a\u9632\u6b62\u91cd\u6392\u5e8f + \u4fdd\u8bc1\u53ef\u89c1\u6027<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. \u7981\u6b62\u6307\u4ee4\u91cd\u6392\u5e8f<\/h3>\n\n\n\n<p>\u52a0\u4e0a <code>volatile<\/code> \u540e\uff0c<code>instance = new Singleton();<\/code> \u4e0d\u5141\u8bb8\u53d1\u751f <strong>\u6784\u9020\u5b8c\u6210\u4e4b\u524d\u5c31\u8d4b\u503c\u5f15\u7528<\/strong> \u7684\u64cd\u4f5c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>private static volatile Singleton instance;<\/code><\/pre>\n\n\n\n<p>JVM \u5c42\u9762\u4f1a\u901a\u8fc7\u5185\u5b58\u5c4f\u969c\u6307\u4ee4\uff08Memory Barrier\uff09\u963b\u6b62\u6307\u4ee4\u91cd\u6392\u5e8f\uff0c\u786e\u4fdd\u521d\u59cb\u5316\u5b8c\u6210\u4e4b\u540e\uff0c\u624d\u628a\u5bf9\u8c61\u5f15\u7528\u8d4b\u503c\u7ed9 <code>instance<\/code>\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. \u4fdd\u8bc1\u7ebf\u7a0b\u95f4\u53ef\u89c1\u6027<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>volatile<\/code> \u80fd\u786e\u4fdd\u4e00\u4e2a\u7ebf\u7a0b\u5bf9 <code>instance<\/code> \u7684\u5199\u5165\uff0c\u5bf9\u5176\u4ed6\u7ebf\u7a0b\u53ef\u89c1\uff1b<\/li>\n\n\n\n<li>\u6ca1\u6709 <code>volatile<\/code>\uff0c\u5176\u4ed6\u7ebf\u7a0b\u53ef\u80fd\u8bfb\u5230\u201c\u65e7\u503c\u201d\u6216\u8005\u672a\u521d\u59cb\u5316\u503c\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u56db\u3001\u52a0\u4e0a <code>volatile<\/code> \u7684\u6b63\u786e DCL \u5b9e\u73b0<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>public class Singleton {\n    private static volatile Singleton instance;\n\n    private Singleton() {}\n\n    public static Singleton getInstance() {\n        if (instance == null) {  \/\/ \u7b2c\u4e00\u6b21\u68c0\u67e5\n            synchronized (Singleton.class) {\n                if (instance == null) {  \/\/ \u7b2c\u4e8c\u6b21\u68c0\u67e5\n                    instance = new Singleton();\n                }\n            }\n        }\n        return instance;\n    }\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e94\u3001\u603b\u7ed3<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u539f\u56e0<\/th><th>\u8bf4\u660e<\/th><\/tr><\/thead><tbody><tr><td>\u6307\u4ee4\u91cd\u6392\u5e8f<\/td><td><code>new Singleton()<\/code> \u4f1a\u88ab\u91cd\u6392\u5e8f\uff0c\u53ef\u80fd\u8fd4\u56de\u672a\u521d\u59cb\u5316\u5bf9\u8c61<\/td><\/tr><tr><td>\u591a\u7ebf\u7a0b\u53ef\u89c1\u6027<\/td><td>\u6ca1\u6709 <code>volatile<\/code>\uff0c\u5176\u4ed6\u7ebf\u7a0b\u53ef\u80fd\u770b\u4e0d\u5230\u6700\u65b0\u503c<\/td><\/tr><tr><td><code>volatile<\/code> \u7684\u4f5c\u7528<\/td><td>\u7981\u6b62\u91cd\u6392\u5e8f\uff0c\u786e\u4fdd\u521d\u59cb\u5316\u5b89\u5168\uff0c\u4fdd\u8bc1\u5185\u5b58\u53ef\u89c1\u6027<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u516d\u3001\u6743\u5a01\u8d44\u6599\u53c2\u8003<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/docs.oracle.com\/javase\/tutorial\/essential\/concurrency\/volatile.html\">Java \u5b98\u65b9\u6587\u6863 &#8211; volatile<\/a><\/li>\n\n\n\n<li>\u300aJava\u5e76\u53d1\u7f16\u7a0b\u5b9e\u6218\u300b &#8211; \u7b2c 3\u300116 \u7ae0<\/li>\n\n\n\n<li><a href=\"https:\/\/www.cs.umd.edu\/~pugh\/java\/memoryModel\/\">JMM\uff08Java \u5185\u5b58\u6a21\u578b\uff09\u5b98\u65b9\u6587\u6863<\/a><\/li>\n\n\n\n<li>Doug Lea \u7684\u300aThe Java Memory Model and Thread\u300b<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>DCL\uff08Double-Checked Locking\uff0c\u53cc\u91cd\u68c0\u67e5\u9501\uff09\u5355\u4f8b\u6a21\u5f0f\u4e2d\uff0cvolatile \u5173\u952e\u5b57\u5bf9\u4e8e\u4fdd\u8bc1 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3973,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[68,86],"tags":[],"class_list":["post-3972","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","category-java-"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3972","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=3972"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3972\/revisions"}],"predecessor-version":[{"id":3974,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3972\/revisions\/3974"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/3973"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=3972"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=3972"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=3972"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}