[Java] JAVA 单例 防止其他人new对象 →→→→→进入此内容的聊天室

来自 , 2021-03-02, 写在 Java, 查看 156 次.
URL http://www.code666.cn/view/8ce6790c
  1. public class Singleton {  
  2.     private static volatile Singleton instance = null;  
  3.     /**
  4.      * 防止其他人new对象
  5.      */  
  6.     private Singleton(){  
  7.         System.out.println("init");  
  8.     }  
  9.     public static  Singleton getInstance(){  
  10.         if(instance == null){  
  11.             synchronized(Singleton.class){  
  12.                 if(instance == null){  
  13.                     instance = new Singleton();  
  14.                 }  
  15.             }  
  16.         }  
  17.         return instance;  
  18.     }  
  19. } //源代码片段来自云代码http://yuncode.net
  20.                        

回复 "JAVA 单例 防止其他人new对象"

这儿你可以回复上面这条便签

captcha