/**
* 单例模式。懒汉式。
*/
public class Singleton2 {
private static Singleton2 instance = null;
private Singleton2(){
}
public static Singleton2 getInstance(){
if(instance == null){
synchronized (Singleton2.class) {
// 这里为什么要对instance进行两次判空呢?面试常考哦!
if(instance == null){
instance = new Singleton2();
}
}
}
return instance;
}
}
单例设计模式(懒汉式)
最新推荐文章于 2025-06-29 17:52:53 发布