首页 > 编程语言 > 详细

Java设计模式——单例模式

时间:2021-08-30 13:35:40      阅读:7      评论:0      收藏:0      [点我收藏+]
 1 package com;
 2 
 3 public class LazySingletonTest {
 4     public static void main(String[] args) {
 5         LazySingleton inst1 = LazySingleton.getInstance();
 6         LazySingleton inst2 = LazySingleton.getInstance();
 7         System.out.println(inst1 == inst2);
 8     }
 9 }
10 
11 class LazySingleton{
12     private static LazySingleton instance;
13     private LazySingleton(){
14 
15     }
16 
17     public static LazySingleton getInstance() {
18         if(instance==null){
19             synchronized (LazySingleton.class) {
20                 if(instance==null)
21                     instance = new LazySingleton();
22             }
23         }
24         return instance;
25     }
26 }

 

Java设计模式——单例模式

原文:https://www.cnblogs.com/zhj271934/p/15202969.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!