{"id":1307,"date":"2024-08-06T14:17:29","date_gmt":"2024-08-06T06:17:29","guid":{"rendered":"http:\/\/www.spiceandwolf.com.cn\/?p=1307"},"modified":"2024-10-15T11:44:18","modified_gmt":"2024-10-15T03:44:18","slug":"singleton-doublechecklock","status":"publish","type":"post","link":"http:\/\/www.spiceandwolf.com.cn\/index.php\/singleton-doublechecklock\/","title":{"rendered":"\u5355\u4f8b\u6a21\u5f0f\u53cc\u91cd\u68c0\u67e5\u9501\u8be6\u89e3"},"content":{"rendered":"\n<p>\u5355\u4f8b\u6a21\u5f0f\u7684\u61d2\u6c49\u5f0f\u7684\u6700\u5927\u95ee\u9898\u5c31\u662f\u7ebf\u7a0b\u5b89\u5168\u6027\u4fdd\u969c\uff0c\u800c\u4e3a\u4e86\u5b9e\u73b0\u7ebf\u7a0b\u5b89\u5168\u6027\uff0c\u61d2\u6c49\u5f0f\u7684\u6700\u7ecf\u5178\u5b9e\u73b0\u5c31\u662f\u53cc\u91cd\u68c0\u67e5\u9501\u3002\u8fd9\u91cc\u7528\u56fe\u8868\u7684\u5f62\u5f0f\u5206\u6790\u4e00\u4e0b\u4e3a\u5565\u662f\u53cc\u91cd\u7684\uff0c\u4ee5\u53ca\u4e3a\u4f55\u5355\u4f8b\u9759\u6001\u53d8\u91cf\u9700\u8981\u52a0\u4e0avolatile\u4fee\u9970\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u4e3a\u4ec0\u4e48\u662f&#8221;\u53cc\u91cd&#8221;\u68c0\u67e5\u9501\uff1f<\/h4>\n\n\n\n<p>\u4e0b\u9762\u4ee3\u7801\u53ea\u6709\u5355\u91cd\u5916\u5c42\u68c0\u67e5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class SingleCheckLockSingleton {\n    private static volatile SingleCheckLockSingleton instance;\n\n    private SingleCheckLockSingleton() {}\n\n    public static SingleCheckLockSingleton getInstance() {\n        if (instance == null) {\n            \/\/ \u6a21\u62df\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u7ade\u4e89\u9501\u7684\u60c5\u51b5\n            try {\n                TimeUnit.MILLISECONDS.sleep(1);\n            } catch (InterruptedException e) {\n                throw new RuntimeException(e);\n            }\n            synchronized (SingleCheckLockSingleton.class) {\n                instance = new SingleCheckLockSingleton();\n            }\n        }\n        return instance;\n    }\n\n    public static void main(String&#91;] args) {\n        for (int i = 1; i &lt;= 5; i++) {\n            new Thread(() -&gt; {\n                SingleCheckLockSingleton ins = getInstance();\n                System.out.println(Thread.currentThread().getName() + \"\\t:\\t\" + ins);\n            }, \"Thread\" + String.valueOf(i)).start();\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"406\" src=\"http:\/\/www.spiceandwolf.com.cn\/wp-content\/uploads\/2024\/08\/image-1024x406.png\" alt=\"\" class=\"wp-image-1308\" srcset=\"http:\/\/www.spiceandwolf.com.cn\/wp-content\/uploads\/2024\/08\/image-1024x406.png 1024w, http:\/\/www.spiceandwolf.com.cn\/wp-content\/uploads\/2024\/08\/image-300x119.png 300w, http:\/\/www.spiceandwolf.com.cn\/wp-content\/uploads\/2024\/08\/image-768x305.png 768w, http:\/\/www.spiceandwolf.com.cn\/wp-content\/uploads\/2024\/08\/image.png 1300w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>\u4e3a\u4e86\u6a21\u62df\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u8fdb\u5165instance == null\u8bed\u53e5\u65f6\u7684\u573a\u666f\uff0c\u6211\u4eec\u5728\u8bed\u53e5\u5185\u505a\u4e86\u77ed\u6682\u7684\u7761\u7720\u64cd\u4f5c\u3002\u53ef\u4ee5\u9884\u6599\u5230\u5f53\u524d\u4f1a\u6709\u591a\u4e2a\u7ebf\u7a0b\u8fdb\u5165\u6761\u4ef6\u5224\u65ad\u8bed\u53e5\u4e2d\uff0c\u4f1a\u521b\u5efa\u591a\u4e2a\u5b9e\u4f8b\uff0c\u5e76\u4e14\u6bcf\u6b21\u53d6\u51fa\u6765\u7684\u5b9e\u4f8b\u90fd\u4e0d\u4e00\u5b9a\u662f\u540c\u4e00\u4e2a\uff0c\u8fd9\u6837\u80af\u5b9a\u662f\u4e0d\u884c\u7684\uff0c\u6240\u4ee5\u4e3a\u4e86\u907f\u514d\u591a\u4e2a\u7ebf\u7a0b\u8fdb\u5165\u6761\u4ef6\u5224\u65ad\u8bed\u53e5\u540e\u4f1a\u521b\u5efa\u591a\u4e2a\u5b9e\u4f8b\uff0c\u8fd9\u5c31\u662f\u4e8c\u91cd\u68c0\u67e5\u9501\u4e4b\u6240\u4ee5\u662f\u4e8c\u91cd\u7684\u539f\u56e0\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class DoubleCheckLockSingleton {\n    private static volatile DoubleCheckLockSingleton instance;\n\n    private DoubleCheckLockSingleton() {}\n\n    public static DoubleCheckLockSingleton getInstance() {\n        \/\/ \u4e00\u91cd\u68c0\u67e5\n        if (instance == null) {\n            synchronized (DoubleCheckLockSingleton.class) {\n                \/\/ \u4e8c\u91cd\u68c0\u67e5\n                if (instance == null) {\n                    instance = new DoubleCheckLockSingleton();\n                }\n            }\n        }\n        return instance;\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u52a0\u5165\u4e86\u5185\u5c42\u5224\u65ad\u540e\uff0c\u5373\u4f7f\u591a\u4e2a\u7ebf\u7a0b\u90fd\u8fdb\u5165\u5230\u4e86synchronized\u8bed\u53e5\u5757\u4e2d\uff0c\u4ecd\u7136\u53ea\u4f1a\u6709\u6700\u521d\u8fdb\u5165\u7684\u7ebf\u7a0b\u4f1a\u8fdb\u5165\u5185\u5c42\u5faa\u73af\u5e76\u6267\u884c\u521b\u5efa\u5b9e\u4f8b\u7684\u4ee3\u7801\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u4e3a\u4f55\u8981\u52a0volatile\u4fee\u9970\uff1f<\/h4>\n\n\n\n<p>\u6211\u4eec\u518d\u63d0\u51fa\u4e00\u4e2a\u5047\u8bbe\uff0c\u5982\u679c\u4e0d\u52a0volatile\u5173\u952e\u5b57\u4f1a\u53d1\u751f\u4ec0\u4e48\uff1f\u5373\u63a2\u7a76volatile\u5173\u952e\u5b57\u5728\u8fd9\u8d77\u5230\u4e86\u4ec0\u4e48\u4f5c\u7528\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u53cc\u91cd\u68c0\u67e5\u9501\u7ebf\u7a0b\u540c\u6b65\u65b9\u5f0f\npublic class DoubleCheckLockSingleton {\n    private static DoubleCheckLockSingleton instance;\n\n    private DoubleCheckLockSingleton() {}\n\n    public static DoubleCheckLockSingleton getInstance() {\n        if (instance == null) {\n            synchronized (DoubleCheckLockSingleton.class) {\n                if (instance == null) {\n                    instance = new DoubleCheckLockSingleton();\n                }\n            }\n        }\n        return instance;\n    }\n}<\/code><\/pre>\n\n\n\n<p>getInstance\u65b9\u6cd5\u5bf9\u5e94\u7684\u5b57\u8282\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> 0 getstatic #2 &lt;singletonPattern\/lazyMode\/DoubleCheckLockSingleton.instance : LsingletonPattern\/lazyMode\/DoubleCheckLockSingleton;&gt;\n 3 ifnonnull 37 (+34)\n 6 ldc #3 &lt;singletonPattern\/lazyMode\/DoubleCheckLockSingleton&gt;\n 8 dup\n 9 astore_0\n10 monitorenter\n11 getstatic #2 &lt;singletonPattern\/lazyMode\/DoubleCheckLockSingleton.instance : LsingletonPattern\/lazyMode\/DoubleCheckLockSingleton;&gt;\n14 ifnonnull 27 (+13)\n17 new #3 &lt;singletonPattern\/lazyMode\/DoubleCheckLockSingleton&gt;\n20 dup\n21 invokespecial #4 &lt;singletonPattern\/lazyMode\/DoubleCheckLockSingleton.&lt;init&gt; : ()V&gt;\n24 putstatic #2 &lt;singletonPattern\/lazyMode\/DoubleCheckLockSingleton.instance : LsingletonPattern\/lazyMode\/DoubleCheckLockSingleton;&gt;\n27 aload_0\n28 monitorexit\n29 goto 37 (+8)\n32 astore_1\n33 aload_0\n34 monitorexit\n35 aload_1\n36 athrow\n37 getstatic #2 &lt;singletonPattern\/lazyMode\/DoubleCheckLockSingleton.instance : LsingletonPattern\/lazyMode\/DoubleCheckLockSingleton;&gt;\n40 areturn<\/code><\/pre>\n\n\n\n<p>\u4e0d\u52a0volatile\u65f6\u7684\u95ee\u9898\u5b9e\u5728\u5f88\u96be\u6210\u529f\u6a21\u62df\u51fa\u6765\uff0c\u6240\u4ee5\u6211\u4eec\u901a\u8fc7\u65f6\u5e8f\u56fe\u8fdb\u884c\u8bb2\u89e3\uff0c\u9996\u5148\u6211\u4eec\u9700\u8981\u77e5\u9053new\u64cd\u4f5c\u5e76\u4e0d\u662f\u539f\u5b50\u64cd\u4f5c\uff0c\u5176\u5185\u90e8\u5305\u542b\u4e86\u590d\u6570\u6761\u5b57\u8282\u7801\u6307\u4ee4\uff0817\u300120\u300121\u300124\uff09\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>new\uff0c\u5728\u5185\u5b58\u4e2d\u4e3a\u65b0\u521b\u5efa\u7684\u5bf9\u8c61\u5f00\u8f9f\u7a7a\u95f4\uff0c\u5982\u679c\u9996\u6b21\u52a0\u8f7d\u7c7b\u5219\u8fdb\u884c\u7c7b\u52a0\u8f7d\u3002<\/li>\n\n\n\n<li>dup\uff0c\u8d4b\u503c\u64cd\u4f5c\u6570\u6808\u6808\u9876\u6570\u636e\u5e76\u538b\u5165\u6808\u9876\u3002<\/li>\n\n\n\n<li>invokespecial\uff0c\u8c03\u7528\u6784\u9020\u65b9\u6cd5\u8fdb\u884c\u7c7b\u521d\u59cb\u5316\u3002<\/li>\n\n\n\n<li>putstatic\uff0c\u5c06\u521b\u5efa\u7684\u5bf9\u8c61\u5730\u5740\u8d4b\u503c\u7ed9\u9759\u6001\u53d8\u91cfinstance\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u800c\u7f16\u8bd1\u5668\u548c\u5904\u7406\u5668\u4f1a\u51fa\u4e8e\u4f18\u5316\u8003\u8651\u5c06\u6307\u4ee4\u91cd\u6392\uff0c\u8fd9\u5c31\u4f1a\u9020\u6210\u8fd9\u51e0\u5929\u6307\u4ee4\u7684\u6267\u884c\u987a\u5e8f\u6539\u53d8\u7684\u60c5\u51b5\uff0c\u5148\u8ba8\u8bbaputstatic\u6307\u4ee4\u5728invokespecial\u4e4b\u524d\u7684\u60c5\u51b5\uff0c\u5373\u5728\u5bf9\u8c61\u8fdb\u884c\u521d\u59cb\u5316\u524d\u5c31\u5df2\u7ecf\u5c06\u5bf9\u8c61\u5730\u5740\u8d4b\u503c\u7ed9instance\u53d8\u91cf\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"873\" src=\"http:\/\/www.spiceandwolf.com.cn\/wp-content\/uploads\/2024\/08\/image-1-1024x873.png\" alt=\"\" class=\"wp-image-1309\" srcset=\"http:\/\/www.spiceandwolf.com.cn\/wp-content\/uploads\/2024\/08\/image-1-1024x873.png 1024w, http:\/\/www.spiceandwolf.com.cn\/wp-content\/uploads\/2024\/08\/image-1-300x256.png 300w, http:\/\/www.spiceandwolf.com.cn\/wp-content\/uploads\/2024\/08\/image-1-768x655.png 768w, http:\/\/www.spiceandwolf.com.cn\/wp-content\/uploads\/2024\/08\/image-1.png 1440w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>\u5728Thread1\u8fd8\u672a\u521d\u59cb\u5316\u5b8c\u5bf9\u8c61\u65f6\uff0cThread2\u5c31\u53ef\u80fd\u62ff\u5230\u5355\u4f8b\u5bf9\u8c61\u5e76\u8fd4\u56de\u4f7f\u7528\uff0c\u8fd9\u65f6\u5bf9\u8c61\u5206\u914d\u7684\u5185\u5b58\u7a7a\u95f4\u4e2d\u53ef\u80fd\u662f\u6ca1\u610f\u4e49\u7684\u6570\u636e\uff0c\u8fd9\u65f6\u5bf9\u8c61\u8bbf\u95ee\u53ef\u80fd\u53d1\u751f\u7a7a\u6307\u9488\u5f02\u5e38\u7b49\u95ee\u9898\u3002<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5355\u4f8b\u6a21\u5f0f\u7684\u61d2\u6c49\u5f0f\u7684\u6700\u5927\u95ee\u9898\u5c31\u662f\u7ebf\u7a0b\u5b89\u5168\u6027\u4fdd\u969c\uff0c\u800c\u4e3a\u4e86\u5b9e\u73b0\u7ebf\u7a0b\u5b89\u5168\u6027\uff0c\u61d2\u6c49\u5f0f\u7684\u6700\u7ecf\u5178\u5b9e\u73b0\u5c31\u662f\u53cc\u91cd\u68c0\u67e5\u9501\u3002\u8fd9\u91cc\u7528\u56fe [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25,80,85],"tags":[],"class_list":["post-1307","post","type-post","status-publish","format-standard","hentry","category-code","category-design-pattern","category-interview"],"_links":{"self":[{"href":"http:\/\/www.spiceandwolf.com.cn\/index.php\/wp-json\/wp\/v2\/posts\/1307","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.spiceandwolf.com.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.spiceandwolf.com.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.spiceandwolf.com.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.spiceandwolf.com.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1307"}],"version-history":[{"count":7,"href":"http:\/\/www.spiceandwolf.com.cn\/index.php\/wp-json\/wp\/v2\/posts\/1307\/revisions"}],"predecessor-version":[{"id":1603,"href":"http:\/\/www.spiceandwolf.com.cn\/index.php\/wp-json\/wp\/v2\/posts\/1307\/revisions\/1603"}],"wp:attachment":[{"href":"http:\/\/www.spiceandwolf.com.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1307"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.spiceandwolf.com.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1307"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.spiceandwolf.com.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1307"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}