{"id":2289,"date":"2018-03-21T16:21:59","date_gmt":"2018-03-21T08:21:59","guid":{"rendered":"https:\/\/ai.zhousir.top\/?p=2289"},"modified":"2026-08-18T12:09:40","modified_gmt":"2026-08-18T04:09:40","slug":"%e5%b8%b8%e8%a7%81%e7%9a%84%e5%87%a0%e7%a7%8d%e5%8d%95%e4%be%8b%e6%a8%a1%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/ai.zhousir.top\/?p=2289","title":{"rendered":"\u5e38\u89c1\u7684\u51e0\u79cd\u5355\u4f8b\u6a21\u5f0f"},"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;\"><strong style=\"color: #4870ac;\">\u5355\u4f8b\u6a21\u5f0f<\/strong>\u662f\u5e38\u7528\u8bbe\u8ba1\u6a21\u5f0f\uff0c\u6838\u5fc3\u7ed3\u6784\u91cc\u53ea\u5305\u542b\u4e00\u4e2a\u88ab\u79f0\u4e3a\u5355\u4f8b\u7684\u7279\u6b8a\u7c7b\uff1a\u4e00\u4e2a\u7c7b\u53ea\u6709\u4e00\u4e2a\u5b9e\u4f8b\uff0c\u4e5f\u5c31\u662f\u53ea\u6709\u4e00\u4e2a\u5bf9\u8c61\u5b9e\u4f8b\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u7cfb\u7edf\u91cc\u6709\u4e9b\u7c7b\u5fc5\u987b\u552f\u4e00\u3002\u4f8b\u5982\u53ef\u4ee5\u6709\u591a\u4e2a\u6253\u5370\u4efb\u52a1\uff0c\u4f46\u540c\u65f6\u53ea\u80fd\u6709\u4e00\u4e2a\u5728\u5de5\u4f5c\uff1b\u552e\u7968\u65f6\u4e00\u5171 100 \u5f20\u7968\u3001\u591a\u4e2a\u7a97\u53e3\u540c\u65f6\u5356\uff0c\u4f59\u91cf\u5fc5\u987b\u662f\u5355\u4f8b\uff0c\u5426\u5219\u4f1a\u8d85\u552e\u3002\u7a97\u53e3\u5bf9\u8c61\u5982\u679c\u4e0d\u552f\u4e00\u5316\uff0c\u4f1a\u5f39\u51fa\u591a\u4e2a\u7a97\u53e3\uff1b\u5185\u5bb9\u76f8\u540c\u5374\u91cd\u590d\u521b\u5efa\uff0c\u5c31\u662f\u6d6a\u8d39\u8d44\u6e90\u3002<\/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\u5e94\u7528\u573a\u666f<\/h2>\n<p style=\"margin: 1em 0px;\">\u6765\u6e90\uff1a\u300a\u5927\u8bdd\u8bbe\u8ba1\u6a21\u5f0f\u300b\u3002\u9700\u6c42\uff1a\u5728\u524d\u7aef\u521b\u5efa\u5de5\u5177\u7bb1\u7a97\u53e3\uff0c\u8981\u4e48\u4e0d\u51fa\u73b0\uff0c\u51fa\u73b0\u4e5f\u53ea\u51fa\u73b0\u4e00\u4e2a\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u9047\u5230\u7684\u95ee\u9898\uff1a\u6bcf\u6b21\u70b9\u51fb\u83dc\u5355\u90fd\u4f1a\u91cd\u590d\u521b\u5efa\u300c\u5de5\u5177\u7bb1\u300d\u7a97\u53e3\u3002\u89e3\u51b3\u65b9\u6848\u4e00\uff1a\u7528 if\uff0c\u6bcf\u6b21\u521b\u5efa\u524d\u5148\u5224\u65ad\u662f\u5426\u4e3a null\uff0c\u4e3a null \u518d new\u3002\u5982\u679c 5 \u4e2a\u5730\u65b9\u90fd\u8981\u5b9e\u4f8b\u51fa\u5de5\u5177\u7bb1\uff0c\u8fd9\u4e2a\u5c0f bug \u8981\u6539 5 \u5904\uff0c\u4ee3\u7801\u91cd\u590d\u3001\u5229\u7528\u7387\u4f4e\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u89e3\u51b3\u65b9\u6848\u4e8c\uff1a\u7528\u5355\u4f8b\u6a21\u5f0f\uff0c\u4fdd\u8bc1\u4e00\u4e2a\u7c7b\u53ea\u6709\u4e00\u4e2a\u5b9e\u4f8b\uff0c\u5e76\u63d0\u4f9b\u4e00\u4e2a\u8bbf\u95ee\u5b83\u7684\u5168\u5c40\u8bbf\u95ee\u70b9\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u4e0b\u9762\u51e0\u79cd\u5199\u6cd5\u4f1a\u53d1\u73b0\uff1a<strong style=\"color: #4870ac;\">\u6240\u6709\u5355\u4f8b\u90fd\u7528\u9759\u6001\u65b9\u6cd5\u521b\u5efa<\/strong>\uff0c\u6240\u4ee5\u5355\u4f8b\u5bf9\u8c61\u5b58\u5728\u5185\u5b58\u7684<strong style=\"color: #4870ac;\">\u9759\u6001\u5171\u4eab\u533a<\/strong>\u3002\uff08\u53ef\u53c2\u8003\uff1a<a href=\"https:\/\/zhidao.baidu.com\/question\/2206072272164938188.html\">https:\/\/zhidao.baidu.com\/question\/2206072272164938188.html<\/a>\uff09<\/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;\">\u4e8c\u3001\u61d2\u6c49\u5f0f\u4e0e\u997f\u6c49\u5f0f<\/h2>\n<ul style=\"padding-left: 1rem; margin-left: 1rem; font-size: 0.9rem; list-style-type: disc;\">\n<li>\n<section>\u61d2\u6c49\u5f0f\uff1a\u7c7b\u52a0\u8f7d\u65f6\u4e0d\u521d\u59cb\u5316\u3002<\/section>\n<\/li>\n<li>\n<section>\u997f\u6c49\u5f0f\uff1a\u7c7b\u52a0\u8f7d\u65f6\u5c31\u5b8c\u6210\u521d\u59cb\u5316\uff0c\u7c7b\u52a0\u8f7d\u6bd4\u8f83\u6162\uff0c\u4f46\u83b7\u53d6\u5bf9\u8c61\u7684\u901f\u5ea6\u5feb\u3002<\/section>\n<\/li>\n<\/ul>\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;\">\u5199\u6cd5<\/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;\">\u7c7b\u578b<\/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;\">\u8981\u70b9<\/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;\">\u7b2c\u4e00\u79cd<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u61d2\u6c49\uff0c\u7ebf\u7a0b\u4e0d\u5b89\u5168<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">lazy loading \u660e\u663e\uff0c\u591a\u7ebf\u7a0b\u4e0d\u80fd\u6b63\u5e38\u5de5\u4f5c<\/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;\">\u7b2c\u4e8c\u79cd<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u61d2\u6c49\uff0c\u7ebf\u7a0b\u5b89\u5168<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">getInstance \u52a0 synchronized\uff0c\u6548\u7387\u4f4e<\/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;\">\u7b2c\u4e09\u79cd<\/td>\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\u88c5\u8f7d\u65f6\u5b9e\u4f8b\u5316\uff0c\u65e0 lazy loading<\/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;\">\u7b2c\u56db\u79cd<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u997f\u6c49\u53d8\u79cd<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u9759\u6001\u5757\u521d\u59cb\u5316\uff0c\u6548\u679c\u540c\u7b2c\u4e09\u79cd<\/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;\">\u7b2c\u4e94\u79cd<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u9759\u6001\u5185\u90e8\u7c7b<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u8c03\u7528 getInstance \u624d\u88c5\u8f7d\u5185\u90e8\u7c7b<\/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;\">\u7b2c\u516d\u79cd<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u679a\u4e3e<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">Effective Java \u63d0\u5021\uff0c\u9632\u53cd\u5e8f\u5217\u5316<\/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;\">\u7b2c\u4e03\u79cd<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u53cc\u91cd\u6821\u9a8c\u9501<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">JDK 1.5 \u4e4b\u540e\u624d\u53ef\u9760<\/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\u4e03\u79cd\u5199\u6cd5<\/h2>\n<h3 style=\"padding: 0px; color: #4870ac; margin: 1.2em 0px 1em; font-size: 1.3em;\">\u7b2c\u4e00\u79cd\uff08\u61d2\u6c49\uff0c\u7ebf\u7a0b\u4e0d\u5b89\u5168\uff09<\/h3>\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 SingletonDemo1 {\n    private static SingletonDemo1 instance;\n    private SingletonDemo1(){}\n    public static SingletonDemo1 getInstance(){\n        if (instance == null) {\n            instance = new SingletonDemo1();\n        }\n        return instance;\n    }\n}<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">\u8fd9\u79cd\u5199\u6cd5 lazy loading \u5f88\u660e\u663e\uff0c\u4f46\u662f\u81f4\u547d\u7684\u662f\u5728\u591a\u7ebf\u7a0b\u4e0d\u80fd\u6b63\u5e38\u5de5\u4f5c\u3002<\/p>\n<h3 style=\"padding: 0px; color: #4870ac; margin: 1.2em 0px 1em; font-size: 1.3em;\">\u7b2c\u4e8c\u79cd\uff08\u61d2\u6c49\uff0c\u7ebf\u7a0b\u5b89\u5168\uff09<\/h3>\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 SingletonDemo2 {\n    private static SingletonDemo2 instance;\n    private SingletonDemo2(){}\n    public static synchronized SingletonDemo2 getInstance(){\n        if (instance == null) {\n            instance = new SingletonDemo2();\n        }\n        return instance;\n    }\n}<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">\u5728 <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> \u91cc\u52a0\u4e86 synchronized\u3002\u591a\u7ebf\u7a0b\u80fd\u5de5\u4f5c\uff0c\u4e5f\u6709 lazy loading\uff0c\u4f46\u56e0\u4e3a\u9501\u6548\u7387\u5f88\u4f4e\uff0c\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u5e76\u4e0d\u9700\u8981\u540c\u6b65\u3002<\/p>\n<h3 style=\"padding: 0px; color: #4870ac; margin: 1.2em 0px 1em; font-size: 1.3em;\">\u7b2c\u4e09\u79cd\uff08\u997f\u6c49\uff09<\/h3>\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 SingletonDemo3 {\n    private static SingletonDemo3 instance = new SingletonDemo3();\n    private SingletonDemo3(){}\n    public static SingletonDemo3 getInstance(){\n        return instance;\n    }\n}<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">\u57fa\u4e8e classloader \u673a\u5236\u907f\u514d\u4e86\u591a\u7ebf\u7a0b\u540c\u6b65\u95ee\u9898\u3002\u4e0d\u8fc7 instance \u5728\u7c7b\u88c5\u8f7d\u65f6\u5c31\u5b9e\u4f8b\u5316\uff0c\u6ca1\u6709\u8fbe\u5230 lazy loading \u7684\u6548\u679c\u3002<\/p>\n<h3 style=\"padding: 0px; color: #4870ac; margin: 1.2em 0px 1em; font-size: 1.3em;\">\u7b2c\u56db\u79cd\uff08\u997f\u6c49\uff0c\u53d8\u79cd\uff09<\/h3>\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 SingletonDemo4 {\n    private static SingletonDemo4 instance = null;\n    static{\n        instance = new SingletonDemo4();\n    }\n    private SingletonDemo4(){}\n    public static SingletonDemo4 getInstance(){\n        return instance;\n    }\n}<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">\u8868\u9762\u5dee\u522b\u633a\u5927\uff0c\u5176\u5b9e\u548c\u7b2c\u4e09\u79cd\u5dee\u4e0d\u591a\uff0c\u90fd\u662f\u7c7b\u521d\u59cb\u5316\u5373\u5b9e\u4f8b\u5316 instance\u3002<\/p>\n<h3 style=\"padding: 0px; color: #4870ac; margin: 1.2em 0px 1em; font-size: 1.3em;\">\u7b2c\u4e94\u79cd\uff08\u9759\u6001\u5185\u90e8\u7c7b\uff09<\/h3>\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 SingletonDemo5 {\n    private static class SingletonHolder{\n        private static final SingletonDemo5 instance = new SingletonDemo5();\n    }\n    private SingletonDemo5(){}\n    public static final SingletonDemo5 getInsatance(){\n        return SingletonHolder.instance;\n    }\n}<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">\u540c\u6837\u5229\u7528 classloader \u4fdd\u8bc1\u521d\u59cb\u5316 instance \u65f6\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u3002\u548c\u7b2c\u4e09\u3001\u56db\u79cd\u7684\u7ec6\u5fae\u5dee\u522b\uff1a\u90a3\u4e24\u79cd\u53ea\u8981 Singleton \u7c7b\u88ab\u88c5\u8f7d\uff0cinstance \u5c31\u4f1a\u88ab\u5b9e\u4f8b\u5316\uff08\u6ca1\u6709 lazy loading\uff09\uff1b\u8fd9\u79cd\u65b9\u5f0f Singleton \u88ab\u88c5\u8f7d\u4e86\uff0cinstance \u4e0d\u4e00\u5b9a\u521d\u59cb\u5316\u3002\u56e0\u4e3a SingletonHolder \u6ca1\u6709\u88ab\u4e3b\u52a8\u4f7f\u7528\uff0c\u53ea\u6709\u663e\u5f0f\u8c03\u7528 getInstance \u624d\u4f1a\u88c5\u8f7d\u5185\u90e8\u7c7b\uff0c\u4ece\u800c\u5b9e\u4f8b\u5316\u3002\u5982\u679c\u5b9e\u4f8b\u5316\u5f88\u8017\u8d44\u6e90\u3001\u60f3\u5ef6\u8fdf\u52a0\u8f7d\uff0c\u53c8\u4e0d\u5e0c\u671b Singleton \u5728\u522b\u5904\u88ab\u7528\u5230\u65f6\u5c31\u63d0\u524d new\uff0c\u8fd9\u79cd\u65b9\u5f0f\u6bd4\u7b2c\u4e09\u3001\u56db\u79cd\u66f4\u5408\u7406\u3002<\/p>\n<h3 style=\"padding: 0px; color: #4870ac; margin: 1.2em 0px 1em; font-size: 1.3em;\">\u7b2c\u516d\u79cd\uff08\u679a\u4e3e\uff09<\/h3>\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 enum SingletonDemo6 {\n    instance;\n    public void whateverMethod(){\n    }\n}<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">\u8fd9\u662f Effective Java \u4f5c\u8005 Josh Bloch \u63d0\u5021\u7684\u65b9\u5f0f\uff1a\u80fd\u907f\u514d\u591a\u7ebf\u7a0b\u540c\u6b65\u95ee\u9898\uff0c\u8fd8\u80fd\u9632\u6b62\u53cd\u5e8f\u5217\u5316\u91cd\u65b0\u521b\u5efa\u5bf9\u8c61\u3002\u4e0d\u8fc7 enum \u662f 1.5 \u624d\u52a0\u5165\u7684\u7279\u6027\uff0c\u5b9e\u9645\u5de5\u4f5c\u4e2d\u5f88\u5c11\u770b\u89c1\u6709\u4eba\u8fd9\u4e48\u5199\u3002<\/p>\n<h3 style=\"padding: 0px; color: #4870ac; margin: 1.2em 0px 1em; font-size: 1.3em;\">\u7b2c\u4e03\u79cd\uff08\u53cc\u91cd\u6821\u9a8c\u9501\uff09<\/h3>\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 SingletonDemo7 {\n    private volatile static SingletonDemo7 singletonDemo7;\n    private SingletonDemo7(){}\n    public static SingletonDemo7 getSingletonDemo7(){\n        if (singletonDemo7 == null) {\n            synchronized (SingletonDemo7.class) {\n                if (singletonDemo7 == null) {\n                    singletonDemo7 = new SingletonDemo7();\n                }\n            }\n        }\n        return singletonDemo7;\n    }\n}<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">\u8fd9\u662f\u7b2c\u4e8c\u79cd\u7684\u5347\u7ea7\u7248\uff0c\u4fd7\u79f0\u53cc\u91cd\u68c0\u67e5\u9501\u5b9a\u3002\u8be6\u7ec6\u4ecb\u7ecd\uff1a<a href=\"http:\/\/www.ibm.com\/developerworks\/cn\/java\/j-dcl.html\">http:\/\/www.ibm.com\/developerworks\/cn\/java\/j-dcl.html<\/a>\u3002\u5728 JDK 1.5 \u4e4b\u540e\uff0c\u53cc\u91cd\u68c0\u67e5\u9501\u5b9a\u624d\u80fd\u591f\u6b63\u5e38\u8fbe\u5230\u5355\u4f8b\u6548\u679c\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u8f6c\u81f3\uff1a<a href=\"https:\/\/www.cnblogs.com\/Ycheng\/p\/7169381.html\">https:\/\/www.cnblogs.com\/Ycheng\/p\/7169381.html<\/a><\/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\u5355\u4f8b\u4fdd\u8bc1\u4e00\u4e2a\u7c7b\u53ea\u6709\u4e00\u4e2a\u5b9e\u4f8b\uff1b\u61d2\u6c49\u5ef6\u8fdf\u521b\u5efa\u3001\u997f\u6c49\u7c7b\u52a0\u8f7d\u5373\u521b\u5efa\uff0c\u5e38\u7528\u9759\u6001\u5185\u90e8\u7c7b\u6216\u53cc\u91cd\u6821\u9a8c\u9501\uff0c\u679a\u4e3e\u8fd8\u80fd\u9632\u53cd\u5e8f\u5217\u5316\u3002<\/strong><\/p>\n<\/blockquote>\n<\/section>\n","protected":false},"excerpt":{"rendered":"<p>\u5355\u4f8b\u6a21\u5f0f\u662f\u5e38\u7528\u8bbe\u8ba1\u6a21\u5f0f\uff0c\u6838\u5fc3\u7ed3\u6784\u91cc\u53ea\u5305\u542b\u4e00\u4e2a\u88ab\u79f0\u4e3a\u5355\u4f8b\u7684\u7279\u6b8a\u7c7b\uff1a\u4e00\u4e2a\u7c7b\u53ea\u6709\u4e00\u4e2a\u5b9e\u4f8b\uff0c\u4e5f\u5c31\u662f\u53ea\u6709\u4e00\u4e2a\u5bf9\u8c61\u5b9e\u4f8b\u3002  [&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\/2289"}],"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=2289"}],"version-history":[{"count":3,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/2289\/revisions"}],"predecessor-version":[{"id":2292,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/2289\/revisions\/2292"}],"wp:attachment":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2289"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2289"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2289"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}