标签:scene pat word 懒加载 文章 pattern 推荐 ESS 环境
Bean
实例就是一个单例对象。public class Singleton {
private static Singleton instance;
private Singleton (){}
public static Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}
synchronized
,实现如下:public class Singleton {
private static Singleton instance;
private Singleton (){}
public static synchronized Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}
public class Singleton {
private static Singleton instance = new Singleton();
private Singleton (){}
public static Singleton getInstance() {
return instance;
}
}
public class Singleton {
private volatile static Singleton singleton;
private Singleton (){}
public static Singleton getSingleton() {
if (singleton == null) {
synchronized (Singleton.class) {
if (singleton == null) {
singleton = new Singleton();
}
}
}
return singleton;
}
}
public class Singleton {
private static class SingletonHolder {
private static final Singleton INSTANCE = new Singleton();
}
private Singleton (){}
public static final Singleton getInstance() {
return SingletonHolder.INSTANCE;
}
}
懒汉式线程不安全
、懒汉式线程安全
、饿汉式
、双重校验锁的实现
、静态内部类的实现
。这几种方式各有优缺点,陈某推荐使用静态内部类的实现方式。Bean
的单实例才能管理和监控Bean的创建和销毁。标签:scene pat word 懒加载 文章 pattern 推荐 ESS 环境
原文地址:https://www.cnblogs.com/Chenjiabing/p/12631368.html