{"id":635,"date":"2015-05-07T17:43:03","date_gmt":"2015-05-07T09:43:03","guid":{"rendered":"https:\/\/ai.zhousir.top\/?p=635"},"modified":"2026-08-18T12:09:32","modified_gmt":"2026-08-18T04:09:32","slug":"%e8%ae%be%e8%ae%a1%e6%a8%a1%e5%bc%8f%e4%b9%8bsingleton%e5%8d%95%e6%80%81%e5%8d%95%e4%be%8b","status":"publish","type":"post","link":"https:\/\/ai.zhousir.top\/?p=635","title":{"rendered":"\u8bbe\u8ba1\u6a21\u5f0f\u4e4bSingleton(\u5355\u6001\/\u5355\u4f8b)"},"content":{"rendered":"<section id=\"wenyan\" style=\"font-family: system-ui, 'Apple Color Emoji', 'Segoe UI', 'Segoe UI Symbol', 'Noto Sans', Roboto, sans-serif; line-height: 1.75; font-size: 16px; color: #000000; caret-color: #000000;\" data-provider=\"WenYan\">\n<p style=\"margin: 1em 0px;\">Singleton\uff08\u5355\u6001 \/ \u5355\u4f8b\uff09\u4fdd\u8bc1\u4e00\u4e2a Class \u5728 Java \u5e94\u7528\u91cc<strong style=\"color: #4870ac;\">\u53ea\u6709\u4e00\u4e2a\u5b9e\u4f8b<\/strong>\u3002\u5efa\u76ee\u5f55\u3001\u62ff\u6570\u636e\u5e93\u8fde\u63a5\u8fd9\u7c7b\u5e0c\u671b\u5168\u5c40\u552f\u4e00\u7684\u8d44\u6e90\uff0c\u5e38\u7528\u5b83\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u5355\u4f8b\u53ef\u4ee5\u5e26\u72b6\u6001\uff1a\u591a\u4e2a\u5355\u6001\u51d1\u5728\u4e00\u8d77\u5c31\u50cf\u72b6\u6001\u4ed3\u5e93\u3002\u4f8b\u5982\u8bba\u575b\u5e16\u5b50\u8ba1\u6570\u5668\uff0c\u6bcf\u6b21\u6d4f\u89c8\u52a0 1\uff0c\u7528 synchronize \u4fdd\u8bc1\u5b89\u5168\uff1b\u82e5\u8981\u6301\u4e45\u5316\u5230\u6570\u636e\u5e93\uff0c\u53ef\u4ee5\u5728\u4e0d\u6539\u5bf9\u5916\u63a5\u53e3\u7684\u524d\u63d0\u4e0b\u52a0\u4e0a\u3002\u4e5f\u53ef\u4ee5\u505a\u6210\u65e0\u72b6\u6001\u7684\u5de5\u5177\u3002\u9650\u5236\u5b9e\u4f8b\u4e2a\u6570\u8fd8\u80fd\u7701\u5185\u5b58\uff0c\u6709\u5229\u4e8e GC\u3002\u5de5\u5382\u3001\u7c7b\u88c5\u5165\u5668\u91cc\u4e5f\u5e38\u89c1\uff0c\u56e0\u4e3a\u88ab\u88c5\u5165\u7684\u7c7b\u672c\u8eab\u5c31\u662f\u8d44\u6e90\u3002<\/p>\n<p><!--more--><\/p>\n<h2 style=\"padding: 1px 12.5px; color: #ffffff; margin: 1.2em 0px 1em; border-radius: 4px; display: inline-block; background-color: #4870ac; font-size: 1.3em;\">\u4e00\u3001\u997f\u6c49\u5f0f\uff1a\u7c7b\u52a0\u8f7d\u65f6\u5c31\u5efa\u597d<\/h2>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">public class Singleton {\n\n    private Singleton() {}\n\n    \/\/ \u5728\u81ea\u5df1\u5185\u90e8\u5b9a\u4e49\u81ea\u5df1\u7684\u5b9e\u4f8b\uff1bprivate \u53ea\u4f9b\u5185\u90e8\u8c03\u7528\n    private static Singleton instance = new Singleton();\n\n    public static Singleton getInstance() {\n        return instance;\n    }\n}<\/code><\/pre>\n<h2 style=\"padding: 1px 12.5px; color: #ffffff; margin: 1.2em 0px 1em; border-radius: 4px; display: inline-block; background-color: #4870ac; font-size: 1.3em;\">\u4e8c\u3001\u61d2\u6c49\u5f0f\uff1a\u7b2c\u4e00\u6b21\u7528\u624d new<\/h2>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">public class Singleton {\n    private static Singleton instance = null;\n\n    public static synchronized Singleton getInstance() {\n        if (instance == null)\n            instance = new Singleton();\n        return instance;\n    }\n}<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">\u5916\u90e8\u4e00\u5f8b <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">Singleton.getInstance()<\/code>\u3002\u7b2c\u4e8c\u79cd\u662f lazy initialization\uff1a\u7b2c\u4e00\u6b21\u8c03\u7528\u624d\u6784\u9020\u3002\u8fd9\u91cc\u7684 <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">synchronized<\/code> \u5f88\u91cd\u8981\uff0c\u53bb\u6389\u5c31\u53ef\u80fd\u62ff\u5230\u591a\u4e2a\u5b9e\u4f8b\u3002\u61d2\u6c49\u8fd8\u6709 double-checked locking\uff08DCL\uff09\u7b49\u8ba8\u8bba\uff0c\u4e00\u822c\u8ba4\u4e3a\u7b2c\u4e00\u79cd\uff08\u997f\u6c49\uff09\u66f4\u5b89\u5168\u4e9b\u3002<\/p>\n<table style=\"display: table; text-align: left; overflow: auto; border-collapse: collapse; margin: 1.4em auto; max-width: 100%; table-layout: fixed; overflow-wrap: break-word; word-break: break-all;\" width=\"800\">\n<thead>\n<tr>\n<th style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top; text-align: center; font-weight: bold; color: #4870ac; background: #f7f7f7;\">\u5f62\u5f0f<\/th>\n<th style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top; text-align: center; font-weight: bold; color: #4870ac; background: #f7f7f7;\">\u5b9e\u4f8b\u4f55\u65f6\u521b\u5efa<\/th>\n<th style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top; text-align: center; font-weight: bold; color: #4870ac; background: #f7f7f7;\">\u6ce8\u610f<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u997f\u6c49<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u7c7b\u52a0\u8f7d\u65f6<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u5b9e\u73b0\u7b80\u5355\uff0c\u4e00\u822c\u66f4\u5b89\u5168<\/td>\n<\/tr>\n<tr>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u61d2\u6c49<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u9996\u6b21 <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">getInstance()<\/code><\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u5fc5\u987b\u540c\u6b65\uff0c\u5426\u5219\u53ef\u80fd\u591a\u5b9e\u4f8b<\/td>\n<\/tr>\n<tr>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">DCL<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u4ecd\u60f3\u61d2\u52a0\u8f7d\u53c8\u5c11\u52a0\u9501<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u7ec6\u8282\u591a\uff0c\u9700\u4e86\u89e3\u5185\u5b58\u6a21\u578b\u540e\u518d\u7528<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2 style=\"padding: 1px 12.5px; color: #ffffff; margin: 1.2em 0px 1em; border-radius: 4px; display: inline-block; background-color: #4870ac; font-size: 1.3em;\">\u4e09\u3001\u5bb9\u5668\u4e0e\u5206\u5e03\u5f0f\u4e0b\u7684\u9677\u9631<\/h2>\n<p style=\"margin: 1em 0px;\">\u591a\u4e2a\u7c7b\u88c5\u5165\u5668\u5404\u81ea\u88c5\u4e00\u4efd\u3001\u6216 EJB \u8de8\u670d\u52a1\u5668\u8de8 JVM \u65f6\uff0cSingleton \u4e0d\u4e00\u5b9a\u201c\u5168\u8fdb\u7a0b\u53ea\u6709\u4e00\u4e2a\u201d\u3002<\/p>\n<p style=\"margin: 1em 0px;\">SUN \u5ba0\u7269\u5e97 Pet Store 1.3.1 \u7684 <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">ServiceLocator<\/code> \u5c31\u662f\u4f8b\u5b50\uff1aEJB \u76ee\u5f55\u548c WEB \u76ee\u5f55\u5404\u6709\u4e00\u4efd\uff0c\u5185\u5bb9\u5dee\u4e0d\u591a\uff0c\u90fd\u505a EJB \u67e5\u8be2\u5b9a\u4f4d\u3002WEB \u4fa7\u91c7\u7528 Singleton\uff08\u8d44\u6e90\u5b9a\u4f4d\u7406\u5e94\u5355\u4f8b\uff09\uff1bEJB \u4fa7\u5355\u4f8b\u5df2\u7ecf\u5931\u6548\uff0c\u6240\u4ee5\u62c6\u6210\u9762\u5411 WEB \u548c\u9762\u5411 EJB \u4e24\u79cd\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u6a21\u5f0f\u770b\u8d77\u6765\u7b80\u5355\uff0c\u8981\u7528\u597d\u9700\u8981\u5bf9\u7c7b\u3001\u7ebf\u7a0b\u3001\u5185\u5b58\u6709\u76f8\u5f53\u4e86\u89e3\u3002\u82e5\u5e94\u7528\u8dd1\u5728\u5bb9\u5668\u91cc\uff0cSingleton \u5b9c\u5c11\u7528\u6216\u6539\u7528\u5bb9\u5668\u63d0\u4f9b\u7684\u66ff\u4ee3\u65b9\u6848\u3002<\/p>\n<blockquote style=\"display: block; font-size: 0.9em; border-left: 3px solid #4870ac; padding: 0.5em 1em; margin: 0px; background: #f6f8fa; color: #40464f;\">\n<p style=\"margin: 1em 0px;\"><strong style=\"color: #4870ac;\">\u4e00\u53e5\u8bdd\u603b\u7ed3\uff1a\u8981\u5168\u5c40\u4e00\u4e2a\u5b9e\u4f8b\u5c31\u7528 Singleton\uff1a\u997f\u6c49\u66f4\u7a33\uff0c\u61d2\u6c49\u5fc5\u987b\u540c\u6b65\uff1b\u8de8 ClassLoader \/ \u8de8 JVM \u65f6\u522b\u5047\u5b9a\u5168\u4e16\u754c\u771f\u7684\u53ea\u6709\u4e00\u4efd\u3002<\/strong><\/p>\n<\/blockquote>\n<\/section>\n","protected":false},"excerpt":{"rendered":"<p>Singleton\uff08\u5355\u6001 \/ \u5355\u4f8b\uff09\u4fdd\u8bc1\u4e00\u4e2a Class \u5728 Java \u5e94\u7528\u91cc\u53ea\u6709\u4e00\u4e2a\u5b9e\u4f8b\u3002\u5efa\u76ee\u5f55\u3001\u62ff\u6570\u636e\u5e93\u8fde [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[58],"tags":[43,179],"_links":{"self":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/635"}],"collection":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=635"}],"version-history":[{"count":1,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/635\/revisions"}],"predecessor-version":[{"id":636,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/635\/revisions\/636"}],"wp:attachment":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=635"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=635"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=635"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}