{"id":882,"date":"2019-11-06T11:30:38","date_gmt":"2019-11-06T03:30:38","guid":{"rendered":"http:\/\/47.101.202.111\/?p=882"},"modified":"2023-04-04T23:09:14","modified_gmt":"2023-04-04T15:09:14","slug":"java%e7%89%b9%e6%80%a7%e5%8d%95%e4%be%8b%e6%a8%a1%e5%bc%8f%e4%b8%8e%e6%9e%9a%e4%b8%be","status":"publish","type":"post","link":"http:\/\/139.196.114.170\/?p=882","title":{"rendered":"\u5355\u4f8b\u6a21\u5f0f &#8211; \u5b89\u5168\u5730\u4fdd\u6709\u4e00\u4efd\u5b9e\u4f8b"},"content":{"rendered":"<h2>\u5355\u4f8b\u6a21\u5f0f<\/h2>\n<p>\u5de5\u5177\u7c7b\u3001\u5185\u5b58\u7c7b\u3001\u7f51\u7edc\u7c7b\u3001\u5927\u6a21\u578b\u9884\u52a0\u8f7d\u7b49\u573a\u666f\u4e2d\uff0c\u516c\u7528\u6027\u7684\u8fd9\u7c7b\u5bf9\u8c61\u4ec5\u9700\u8981\u4e00\u4efd\u5b9e\u4f8b\u5728\u5806\u5185\u5b58\u4e2d\uff0c\u56e0\u6b64\u8bde\u751f\u51fa\u5355\u4f8b\u6a21\u5f0f<\/p>\n<h3>\u7ebf\u7a0b\u5b89\u5168\u7684\u997f\u6c49\u5f0f\u5b9e\u73b0<\/h3>\n<p>\u9759\u6001\u5e38\u91cf\u5728\u7c7b\u52a0\u8f7d\u65f6\u521b\u5efa\uff0c\u4ece\u59cb\u81f3\u7ec8\u4fdd\u6709\u5355\u4f8b<\/p>\n<pre><code class=\"language-java\">public class Singleton {\n    private static final Singleton instance = new Singleton();\n    public static Singleton getInstance() {\n        return instance;\n    }\n    private Singleton() {}\n}<\/code><\/pre>\n<h3>\u7ebf\u7a0b\u4e0d\u5b89\u5168\u7684\u61d2\u6c49\u5f0f\u5b9e\u73b0<\/h3>\n<p>\u61d2\u6c49\u5f0f\u7684\u60f3\u6cd5\u662f\u672a\u4f7f\u7528\u4e0d\u521b\u5efa\uff0c\u53ea\u6709\u7b2c\u4e00\u6b21\u4f7f\u7528\u65f6\u624d\u521b\u5efa\uff0c\u4e4b\u540e\u4e00\u76f4\u4fdd\u6709\u5355\u4f8b\u3002\u4f46\u662f\u6b64\u65b9\u6cd5\u662f\u7ebf\u7a0b\u4e0d\u5b89\u5168\u7684\uff0c\u5f53\u7ebf\u7a0bA\u521a\u521a\u6267\u884c\u5b8c(1)\u65f6\uff0c\u7ebf\u7a0bB\u6b64\u65f6\u6267\u884c\u5230(1)\u8fd8\u5904\u4e8e\u672a\u521b\u5efa\u72b6\u6001\uff0c\u56e0\u6b64\u4e5f\u4f1a\u53bb\u6267\u884c(2)\uff0c\u6700\u7ec8\u5c06\u4f1a\u521b\u5efa\u4e24\u4efd\u5b9e\u4f8b<\/p>\n<pre><code class=\"language-java\">public class Singleton{\n    private static Singleton instance = null;\n    public static Singleton getInstance(){\n        if(instance == null) {          \/\/ (1)\n            instance = new Singleton(); \/\/ (2)\n        }\n        return instance;\n    }\n    private Singleton() {}\n}<\/code><\/pre>\n<h3>\u7ebf\u7a0b\u5b89\u5168\u7684\u61d2\u6c49\u5f0f<\/h3>\n<p>\u65b9\u6cd5\u52a0\u9501\u4fdd\u8bc1\u7ebf\u7a0b\u5b89\u5168\u548c\u5355\u4f8b\uff0c\u9501\u7c92\u5ea6\u8f83\u5927\u9020\u6210\u6027\u80fd\u4e0b\u964d<br \/>\n\u7ebf\u7a0b\u5b89\u5168\u7684\u61d2\u6c49\u5f0f\u53ef\u4ee5\u5f62\u8c61\u5316\u4e3a\u4e00\u4e2a\u573a\u666f\uff1a\u6240\u6709\u4eba\u5728\u95e8\u5916\u6392\u961f\u529e\u4e1a\u52a1\uff0c\u6bcf\u6b21\u53ea\u5141\u8bb8\u8fdb\u5165\u4e00\u4e2a\u4eba\uff0c\u8fdb\u53bb\u540e\u5927\u95e8\u4e0a\u9501\u3002\u5f53\u65e5\u529e\u7406\u540d\u989d\u5df2\u7ecf\u6ee1\u5458\u65f6\uff0c\u6392\u961f\u7684\u6240\u6709\u4eba\u4ecd\u7136\u8981\u4f9d\u6b21\u8fdb\u5927\u95e8\u53bb\u5c1d\u8bd5\u529e\u7406<\/p>\n<pre><code class=\"language-java\">public class Singleton{\n    private static Singleton instance = null;\n    public static synchronized Singleton getInstance(){\n        if(instance == null) {\n            instance = new Singleton();\n        }\n        return instance;\n    }\n    private Singleton() {}\n}<\/code><\/pre>\n<h3>\u53cc\u91cd\u6821\u9a8c\u9501\u61d2\u6c49\u5f0f<\/h3>\n<p>\u51cf\u5c0f\u9501\u7c92\u5ea6\uff0c\u4f18\u5148\u4fdd\u8bc1\u8bfb\u53d6\u6027\u80fd\uff0c\u53ea\u6709\u5199\u65f6\u624d\u52a0\u9501\u3002\u4f7f\u7528volatile\u4e0d\u4ec5\u662f\u4e3a\u4e86\u5199\u64cd\u4f5c\u5bf9\u5176\u4ed6\u7ebf\u7a0b\u7684\u53ef\u89c1\u6027\uff0c\u540c\u65f6\u662f\u4e3a\u4e86\u7981\u6b62\u6307\u4ee4\u91cd\u6392\u5e8f\uff1anew Singleton()\u7684\u5b9e\u73b0\u903b\u8f91\u5e76\u4e0d\u662f\u539f\u5b50\u7684\uff1a\u5206\u914d\u5185\u5b58\u3001\u521d\u59cb\u5316\u3001\u5f15\u7528\u8d4b\u503c\uff0c\u8fd9\u4e09\u4e2a\u64cd\u4f5c\u53ef\u80fd\u88ab\u91cd\u6392\u5e8f\u5148\u5f15\u7528\u8d4b\u503c\uff0c\u4f7f\u5f97if(instance == null)\u6821\u9a8c\u4e0d\u901a\u8fc7\u800c\u8fd4\u56de\u7a7ainstance<\/p>\n<p>\u53cc\u91cd\u6821\u9a8c\u9501\u53ef\u4ee5\u5f62\u8c61\u5316\u4e3a\u4e00\u4e2a\u573a\u666f\uff1a\u6240\u6709\u4eba\u5728\u8fdb\u95e8\u540e\u6392\u961f\u7b49\u5f85\u5230\u4e1a\u52a1\u7a97\u53e3\u529e\u4e1a\u52a1\uff0c\u7a97\u53e3\u6bcf\u6b21\u53ea\u670d\u52a1\u4e00\u4e2a\u4eba\u3002\u5f53\u65e5\u529e\u7406\u540d\u989d\u5df2\u7ecf\u6ee1\u5458\u65f6\uff0c\u53ea\u6709\u6392\u961f\u7684\u4eba\u4f1a\u5c1d\u8bd5\u518d\u53bb\u529e\uff0c\u800c\u65b0\u6765\u7684\u4eba\u5728\u95e8\u53e3\u5c31\u77e5\u9053\u540d\u989d\u5df2\u6ee1\u76f4\u63a5\u79bb\u53bb<\/p>\n<pre><code class=\"language-java\">public class Singleton{\n    private static volatile Singleton instance;\n    public static Singleton getInstance() {\n        if(instance == null) {\n            synchronized(Singleton.class) {\n                if(instance == null) {\n                    instance = new Singleton();\n                }\n            }\n        }\n        return instance;\n    }\n    private Singleton() {}\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5355\u4f8b\u6a21\u5f0f \u5de5\u5177\u7c7b\u3001\u5185\u5b58\u7c7b\u3001\u7f51\u7edc\u7c7b\u3001\u5927\u6a21\u578b\u9884\u52a0\u8f7d\u7b49\u573a\u666f\u4e2d\uff0c\u516c\u7528\u6027\u7684\u8fd9\u7c7b\u5bf9\u8c61\u4ec5\u9700\u8981\u4e00\u4efd\u5b9e\u4f8b\u5728\u5806\u5185\u5b58\u4e2d\uff0c\u56e0\u6b64\u8bde\u751f\u51fa\u5355 &hellip; <\/p>\n<p class=\"link-more\"><a href=\"http:\/\/139.196.114.170\/?p=882\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">\u201c\u5355\u4f8b\u6a21\u5f0f &#8211; \u5b89\u5168\u5730\u4fdd\u6709\u4e00\u4efd\u5b9e\u4f8b\u201d<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[31],"tags":[],"jetpack_featured_media_url":"","_links":{"self":[{"href":"http:\/\/139.196.114.170\/index.php?rest_route=\/wp\/v2\/posts\/882"}],"collection":[{"href":"http:\/\/139.196.114.170\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/139.196.114.170\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/139.196.114.170\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/139.196.114.170\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=882"}],"version-history":[{"count":10,"href":"http:\/\/139.196.114.170\/index.php?rest_route=\/wp\/v2\/posts\/882\/revisions"}],"predecessor-version":[{"id":2723,"href":"http:\/\/139.196.114.170\/index.php?rest_route=\/wp\/v2\/posts\/882\/revisions\/2723"}],"wp:attachment":[{"href":"http:\/\/139.196.114.170\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=882"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/139.196.114.170\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=882"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/139.196.114.170\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=882"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}