单例模式,简言之,一个类只有一个实例
懒汉(线程不安全)
存在线程安全问题,多线程并发调用 getInstance 可能会创建多个实例。
class Singleton {
private static Singleton instance;
private Singleton() {
}
public static Singleton getInstance() {
if(instance == null) {
instance = new Singleton();
}
return instance;
}
}
懒汉(线程安全,synchronized)
任何时候只能有一个线程调用 getInstance 方法,效率低。事实上,只有在第一次创建实例对象时才需要同步操作。
class Singleton {
private static Singleton instance;
private Singleton() {
}
public static synchronized Singleton getInstance() {
if(instance == null) {
instance = new Singleton();
}
return instance;
}
}
懒汉(线程安全,双重检测+synchronized+volatile)
class Singleton {
private volatile static Singleton instance;
private Singleton() {
}
public static Singleton getInstance() {
if(instance == null) {
synchronized (Singleton.class) {
if(instance == null) {
instance = new Singleton();
}
}
}
return instance;
}
}
- 为啥用 synchronized?避免多线程并发同时判断 instance 是 null 而创建多个实例的情况发生(如线程 A 判断 instance 是 null,此时 CPU 时间片切换,线程 B 又判断 instance 是 null,两个线程就创建了两个不同的实例)
- 为啥有外层判断
if(instance == null) {
?只有第一次创建实例时才需要同步,之后调用 getInstance 无需同步,直接返回创建好的 instance 即可,这避免每次调用 getInstance 都要进行同步(效率太低) - 为啥有内层判断
if(instance == null) {
?避免创建多个实例(如线程 A 判断 instance 是 null,此时 CPU 时间片切换,线程 B 又判断 instance 是 null,线程 B 进入同步代码块创建实例,之后 CPU 时间片切换到线程 A,线程 A 再次创建新实例) - instance 为啥用 volatile 修饰?代码
instance = new Singleton();
有三个操作,1)开票内存空间;2)对象构造初始化;3)instance 由 null 改为对应内存地址。上述三个操作的顺序可能是 1-2-3 或 1-3-2,如果没有 volatile 且顺序是 1-3-2,就有可能执行完 1-3 后,CPU 时间片切换到另一个线程,另一个线程拿到的就是没有完成初始化的 instance,使用时出错! volatile 保证了上述三个操作执行完才能对 instance 进行读操作,原理是在赋值操作后面插入一个内存屏障(生成的汇编代码上),读操作不会被重排序到内存屏障之前(happens-before 原则,对于一个 volatile 变量的写操作都发生于后序对这个变量的读操作之前)。JDK 1.5 之后才保证 volatile 能够进行禁止指令重排序
饿汉(线程安全,优)
如果实例的创建依赖参数或者配置文件,在 getInstance 之前必须调用某个方法设置参数,这种写法就不适用了
class Singleton {
private static final Singleton instance = new Singleton();
private Singleton() {
}
public static Singleton getInstance() {
return instance;
}
}
静态内部类(线程安全,优)
JVM 本身机制保证了线程安全;懒汉式的;不需要同步,没有性能缺陷;也不依赖 JDK 版本。
class Singleton {
private Singleton() {}
private static class SingletonHolder {
private static final Singleton INSTANCE = new Singleton();
}
public static Singleton getInstance() {
return SingletonHolder.INSTANCE;
}
}
枚举(线程安全,优)
创建枚举默认是线程安全的,而且还能防止反序列化导致重新创建新的对象(反序列化漏洞,攻击者自行构造二进制流反序列化成特定对象,如改变 HTTP 请求参数)
enum Singleton {
INSTANCE
}