{"id":10020434,"date":"2024-12-04T10:03:47","date_gmt":"2024-12-04T02:03:47","guid":{"rendered":"https:\/\/itmax.cn\/?p=10020434"},"modified":"2024-12-04T10:03:47","modified_gmt":"2024-12-04T02:03:47","slug":"%e6%89%8b%e6%92%95%e5%8d%95%e4%be%8b%e7%9a%84-5-%e7%a7%8d%e5%86%99%e6%b3%95%ef%bc%81","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10020434","title":{"rendered":"\u624b\u6495\u5355\u4f8b\u7684 5 \u79cd\u5199\u6cd5\uff01"},"content":{"rendered":"<p>\u5355\u4f8b\u6a21\u5f0f\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u8bbe\u8ba1\u6a21\u5f0f\uff0c\u5b83\u786e\u4fdd\u4e00\u4e2a\u7c7b\u53ea\u6709\u4e00\u4e2a\u5b9e\u4f8b\uff0c\u5e76\u63d0\u4f9b\u4e00\u4e2a\u5168\u5c40\u8bbf\u95ee\u70b9\u6765\u83b7\u53d6\u8be5\u5b9e\u4f8b\u3002\u5f53\u7136\uff0c\u5b83\u4e5f\u662f\u9762\u8bd5\u4e2d\u7684\u5e38\u5ba2\uff0c\u5c24\u5176\u662f\u67d0\u624b\u9762\u8bd5\u4e2d\u7ecf\u5e38\u8981\u6c42\u5e94\u8058\u8005\u624b\u6495\uff0c\u6240\u4ee5\u4eca\u5929\u54b1\u4eec\u5c31\u6765\u76d8\u76d8\u5b83\u3002<\/p>\n<p>\u5355\u4f8b\u6a21\u5f0f\u7684\u5b9e\u73b0\u65b9\u5f0f\u6709\u5f88\u591a\uff0c\u5982\u4e0b\u56fe\u6240\u793a\uff1a<\/p>\n<p><img decoding=\"async\"   referrerpolicy=\"no-referrer\" src=\"https:\/\/cdn.nlark.com\/yuque\/0\/2024\/png\/92791\/1733213723485-6ae89689-f2e1-4023-a468-378a80ac87a8.png\" alt=\"\" title=\"\"><\/p>\n<p>\u5177\u4f53\u5b9e\u73b0\u5982\u4e0b\u3002<\/p>\n<h2>1.\u997f\u6c49\u5f0f\u6a21\u5f0f<\/h2>\n<p>\u6b64\u5728\u997f\u6c49\u5f0f\u5355\u4f8b\u6a21\u5f0f\u4e2d\uff0c\u5b9e\u4f8b\u5728\u7c7b\u52a0\u8f7d\u65f6\u5c31\u88ab\u521b\u5efa\uff0c\u8fd9\u79cd\u65b9\u5f0f\u7684\u4f18\u70b9\u662f\u5b9e\u73b0\u7b80\u5355\uff0c\u7ebf\u7a0b\u5b89\u5168\uff08\u56e0\u4e3a\u7c7b\u52a0\u8f7d\u8fc7\u7a0b\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\uff09\u3002\u7f3a\u70b9\u662f\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5b9e\u4f8b\u8fc7\u65e9\u521b\u5efa\uff0c\u5982\u679c\u5b9e\u4f8b\u521b\u5efa\u8fc7\u7a0b\u6bd4\u8f83\u8017\u65f6\u6216\u8005\u5360\u7528\u5927\u91cf\u8d44\u6e90\uff0c\u800c\u5728\u7a0b\u5e8f\u8fd0\u884c\u521d\u671f\u5e76\u4e0d\u9700\u8981\u8be5\u5b9e\u4f8b\uff0c\u5c31\u4f1a\u9020\u6210\u8d44\u6e90\u6d6a\u8d39\u3002<\/p>\n<pre><code class=\"java\">public class Singleton {\n    \/\/ 1.\u79c1\u6709\u9759\u6001\u6210\u5458\u53d8\u91cf\uff0c\u5728\u7c7b\u52a0\u8f7d\u65f6\u5c31\u521b\u5efa\u5b9e\u4f8b\n    private static Singleton instance = new Singleton();\n\n    \/\/ 2.\u79c1\u6709\u6784\u9020\u51fd\u6570\uff0c\u9632\u6b62\u5916\u90e8\u901a\u8fc7\u6784\u9020\u51fd\u6570\u521b\u5efa\u5b9e\u4f8b\n    private Singleton() {}\n\n    \/\/ 3.\u516c\u5171\u9759\u6001\u65b9\u6cd5\uff0c\u7528\u4e8e\u83b7\u53d6\u552f\u4e00\u7684\u5b9e\u4f8b\n    public static Singleton getInstance() {\n        return instance;\n    }\n}<\/code><\/pre>\n<h2>2.\u61d2\u6c49\u6a21\u5f0f\uff08\u975e\u5b89\u5168\uff09<\/h2>\n<p>\u61d2\u6c49\u5f0f\u5355\u4f8b\u6a21\u5f0f\u5728\u7b2c\u4e00\u6b21\u8c03\u7528 getInstance \u65b9\u6cd5\u65f6\u624d\u521b\u5efa\u5b9e\u4f8b\uff0c\u8fd9\u6837\u53ef\u4ee5\u907f\u514d\u5b9e\u4f8b\u8fc7\u65e9\u521b\u5efa\u3002\u4f46\u4e0a\u8ff0\u4ee3\u7801\u662f\u975e\u7ebf\u7a0b\u5b89\u5168\u7684\uff0c\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e0b\uff0c\u53ef\u80fd\u4f1a\u51fa\u73b0\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u8fdb\u5165 if \u8bed\u53e5\uff0c\u5bfc\u81f4\u521b\u5efa\u591a\u4e2a\u5b9e\u4f8b\u7684\u60c5\u51b5\u3002<\/p>\n<pre><code class=\"java\">public class Singleton {\n    \/\/ 1.\u79c1\u6709\u9759\u6001\u6210\u5458\u53d8\u91cf\uff0c\u521d\u59cb\u5316\u4e3anull\n    private static Singleton instance = null;\n\n    \/\/ 2.\u79c1\u6709\u6784\u9020\u51fd\u6570\uff0c\u9632\u6b62\u5916\u90e8\u901a\u8fc7\u6784\u9020\u51fd\u6570\u521b\u5efa\u5b9e\u4f8b\n    private Singleton() {}\n\n    \/\/ 3.\u516c\u5171\u9759\u6001\u65b9\u6cd5\uff0c\u7528\u4e8e\u83b7\u53d6\u552f\u4e00\u7684\u5b9e\u4f8b\n    public static Singleton getInstance() {\n        if (instance == null) {\n            instance = new Singleton();\n        }\n        return instance;\n    }\n}<\/code><\/pre>\n<h2>3.\u61d2\u6c49\u6a21\u5f0f\uff08\u5b89\u5168\u6548\u7387\u4f4e\uff09<\/h2>\n<p>\u6b64\u7248\u672c\u7684\u61d2\u6c49\u5f0f\u5355\u4f8b\u6a21\u5f0f\u901a\u8fc7\u5728 getInstance \u65b9\u6cd5\u4e0a\u6dfb\u52a0 synchronized \u5173\u952e\u5b57\uff0c\u4f7f\u5176\u6210\u4e3a\u7ebf\u7a0b\u5b89\u5168\u7684\u3002\u4f46\u8fd9\u79cd\u65b9\u5f0f\u7684\u7f3a\u70b9\u662f\u6bcf\u6b21\u8c03\u7528 getInstance \u65f6\u90fd\u9700\u8981\u83b7\u53d6\u9501\uff0c\u4f1a\u5bfc\u81f4\u6027\u80fd\u4e0b\u964d\uff0c\u5c24\u5176\u662f\u5728\u9ad8\u5e76\u53d1\u73af\u5883\u4e0b\u3002<\/p>\n<pre><code class=\"java\">public class Singleton {\n    \/\/ 1.\u79c1\u6709\u9759\u6001\u6210\u5458\u53d8\u91cf\uff0c\u521d\u59cb\u5316\u4e3anull\n    private static Singleton instance = null;\n\n    \/\/ 2.\u79c1\u6709\u6784\u9020\u51fd\u6570\uff0c\u9632\u6b62\u5916\u90e8\u901a\u8fc7\u6784\u9020\u51fd\u6570\u521b\u5efa\u5b9e\u4f8b\n    private Singleton() {}\n\n    \/\/ 3.\u516c\u5171\u9759\u6001\u65b9\u6cd5\uff0c\u7528\u4e8e\u83b7\u53d6\u552f\u4e00\u7684\u5b9e\u4f8b\n    public static synchronized Singleton getInstance() {\n        if (instance == null) {\n            instance = new Singleton();\n        }\n        return instance;\n    }\n}<\/code><\/pre>\n<h2>4.\u53cc\u91cd\u68c0\u67e5\u9501\u6a21\u5f0f<\/h2>\n<p>\u53cc\u91cd\u68c0\u67e5\u9501\u5b9a\u6a21\u5f0f\u5728\u61d2\u6c49\u5f0f\u57fa\u7840\u4e0a\u8fdb\u884c\u4e86\u4f18\u5316\uff0c\u901a\u8fc7\u4e24\u6b21\u68c0\u67e5 instance \u662f\u5426\u4e3a null\uff0c\u65e2\u4fdd\u8bc1\u4e86\u5728\u7b2c\u4e00\u6b21\u9700\u8981\u5b9e\u4f8b\u65f6\u521b\u5efa\u5b9e\u4f8b\uff0c\u53c8\u5728\u4e00\u5b9a\u7a0b\u5ea6\u4e0a\u907f\u514d\u4e86\u6bcf\u6b21\u8c03\u7528 getInstance \u90fd\u83b7\u53d6\u9501\u7684\u60c5\u51b5\uff0c\u63d0\u9ad8\u4e86\u6027\u80fd\u3002\u4e0d\u8fc7\uff0c\u7531\u4e8e\u6307\u4ee4\u91cd\u6392\u5e8f\u7b49\u95ee\u9898\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u4e00\u4e9b\u9519\u8bef\uff0c\u56e0\u6b64\u9700\u8981\u5728 instance \u53d8\u91cf\u524d\u6dfb\u52a0 volatile \u5173\u952e\u5b57\u6765\u89e3\u51b3\u3002<\/p>\n<pre><code class=\"java\">public class Singleton {\n    \/\/ 1.\u79c1\u6709\u9759\u6001\u6210\u5458\u53d8\u91cf\uff0c\u521d\u59cb\u5316\u4e3anull\n    private volatile static Singleton instance = null;\n\n    \/\/ 2.\u79c1\u6709\u6784\u9020\u51fd\u6570\uff0c\u9632\u6b62\u5916\u90e8\u901a\u8fc7\u6784\u9020\u51fd\u6570\u521b\u5efa\u5b9e\u4f8b\n    private Singleton() {}\n\n    \/\/ 3.\u516c\u5171\u9759\u6001\u65b9\u6cd5\uff0c\u7528\u4e8e\u83b7\u53d6\u552f\u4e00\u7684\u5b9e\u4f8b\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}<\/code><\/pre>\n<h2>5.\u9759\u6001\u5185\u90e8\u7c7b\u6a21\u5f0f<\/h2>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u5229\u7528\u4e86\u9759\u6001\u5185\u90e8\u7c7b\u7684\u7279\u6027\uff0c\u5f53\u5916\u90e8\u7c7b\u88ab\u52a0\u8f7d\u65f6\uff0c\u9759\u6001\u5185\u90e8\u7c7b\u4e0d\u4f1a\u88ab\u52a0\u8f7d\uff0c\u53ea\u6709\u5f53\u8c03\u7528 getInstance \u65b9\u6cd5\u65f6\uff0c\u9759\u6001\u5185\u90e8\u7c7b\u624d\u4f1a\u88ab\u52a0\u8f7d\uff0c\u6b64\u65f6\u624d\u521b\u5efa\u5355\u4f8b\u5b9e\u4f8b\u3002\u8fd9\u79cd\u5b9e\u73b0\u65b9\u5f0f\u65e2\u4fdd\u8bc1\u4e86\u7ebf\u7a0b\u5b89\u5168\uff0c\u53c8\u907f\u514d\u4e86\u5728\u4e0d\u9700\u8981\u5b9e\u4f8b\u65f6\u8fc7\u65e9\u521b\u5efa\u5b9e\u4f8b\uff0c\u662f\u4e00\u79cd\u6bd4\u8f83\u5e38\u7528\u7684\u5355\u4f8b\u6a21\u5f0f\u5b9e\u73b0\u65b9\u5f0f\u3002<\/p>\n<pre><code class=\"java\">public class Singleton {\n    \/\/ 1.\u79c1\u6709\u6784\u9020\u51fd\u6570\uff0c\u9632\u6b62\u5916\u90e8\u901a\u8fc7\u6784\u9020\u51fd\u6570\u521b\u5efa\u5b9e\u4f8b\n    private Singleton() {}\n\n    \/\/ 2.\u9759\u6001\u5185\u90e8\u7c7b\uff0c\u5176\u4e2d\u5305\u542b\u5355\u4f8b\u5b9e\u4f8b\n    private static class SingletonHolder {\n        private static final Singleton instance = new Singleton();\n    }\n\n    \/\/ 3.\u516c\u5171\u9759\u6001\u65b9\u6cd5\uff0c\u7528\u4e8e\u83b7\u53d6\u552f\u4e00\u7684\u5b9e\u4f8b\n    public static Singleton getInstance() {\n        return SingletonHolder.instance;\n    }\n}<\/code><\/pre>\n<h2>\u5c0f\u7ed3<\/h2>\n<p>\u5355\u4f8b\u6a21\u5f0f\u867d\u7136\u5b9e\u73b0\u65b9\u5f0f\u6709 5  \u79cd\uff1a\u997f\u6c49\u6a21\u5f0f\u3001\u61d2\u6c49\u975e\u5b89\u5168\u6a21\u5f0f\u3001\u61d2\u6c49\u5b89\u5168\u6a21\u5f0f\u3001\u53cc\u91cd\u6548\u9a8c\u9501\u6a21\u5f0f\u3001\u9759\u6001\u5185\u90e8\u7c7b\u6a21\u5f0f\uff0c\u4f46\u5b83\u7684\u5199\u6cd5\u57fa\u672c\u90fd\u662f\u4ee5\u4e0b\u4e09\u6b65\uff1a<\/p>\n<ol>\n<li>\u5b9a\u4e49\u79c1\u6709\u6784\u9020\u65b9\u6cd5\uff08\u9632\u6b62 new \u591a\u4e2a\u5b9e\u4f8b\uff09\u3002<\/li>\n<li>\u5b9a\u4e49\u79c1\u6709\u53d8\u91cf\uff08\u627f\u63a5\u5355\u4f8b\u5bf9\u8c61\uff09\u3002<\/li>\n<li>\u5b9a\u4e49\u7edf\u4e00\u8fd4\u56de\u5bf9\u8c61\u7684\u65b9\u6cd5\u3002<\/li>\n<\/ol>\n<blockquote><p>\u672c\u6587\u5df2\u6536\u5f55\u5230\u6211\u7684\u9762\u8bd5\u5c0f\u7ad9 <a href=\"https:\/\/link.segmentfault.com\/?enc=o0eXBFmqu%2FpvTkrgQ40ATQ%3D%3D.WMFVZyPnMrJksVIwl0tDpDM54P9Hejk2aCXzaksdZKg%3D\" rel=\"nofollow noopener noreferrer\" target=\"_blank\">www.javacn.site<\/a>\uff0c\u5176\u4e2d\u5305\u542b\u7684\u5185\u5bb9\u6709\uff1a\u5e76\u53d1\u7f16\u7a0b\u3001MySQL\u3001Redis\u3001Spring\u3001Spring MVC\u3001Spring Boot\u3001Spring Cloud\u3001MyBatis\u3001JVM\u3001\u8bbe\u8ba1\u6a21\u5f0f\u3001\u6d88\u606f\u961f\u5217\u7b49\u6a21\u5757\u3002<\/p><\/blockquote>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000045535261\">https:\/\/segmentfault.com\/a\/1190000045535261<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5355\u4f8b\u6a21\u5f0f\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u8bbe\u8ba1\u6a21\u5f0f\uff0c\u5b83\u786e\u4fdd\u4e00\u4e2a\u7c7b\u53ea\u6709\u4e00\u4e2a\u5b9e\u4f8b\uff0c\u5e76\u63d0\u4f9b\u4e00\u4e2a\u5168\u5c40\u8bbf\u95ee\u70b9\u6765\u83b7\u53d6\u8be5\u5b9e\u4f8b\u3002\u5f53\u7136\uff0c\u5b83\u4e5f\u662f\u9762\u8bd5\u4e2d\u7684 [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10020434","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10020434","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10020434"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10020434\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10020434"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10020434"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10020434"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}