单例模式(Singleton)
确保一个类只有一个实例,并提供一个全局访问点。
应用场景:线程池、注册表、任务管理器、日志对象、充当打印机、显卡等设备的驱动程序等的对象。
经典的单例模式
class Singleton {
private static Singleton instance;
// 私有的构造器,外部无法 new Singleton()
private Singleton() { }
public static Singleton getInstance() {
// 没有实例才 new
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}
缺点:多线程下有可能 new 出多个不同的实例。
为什么?
假设有两个线程 A 和 B 都调用了方法 getInstance():
- A 判断完 instance 为空,准备 new;此时 B 也对 instance 进行判断,因为 A 还没 new,所以 B 判断也是空,也准备 new。
- A new 完了,B 接着又 new 一个,结果就出现了两个不同的实例。
加上同步锁
class Singleton {
private static Singleton instance;
private Singleton() { }
// 加上 synchronized 同步
// 保证同一时间不会有别的线程进入该方法
public static synchronized Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}
缺点:确实解决了多线程的问题,但是也造成了性能浪费。因为只有第一次 new 时才需要同步,之后每次还进行同步就显得累赘了。
加载时就创建实例
class Singleton {
// JVM 加载该类时就会 new 该实例
private static Singleton instance = new Singleton();
private Singleton() { }
public static Singleton getInstance() {
return instance;
}
}
缺点:如果不是经常使用到的实例,也是会造成性能浪费。
双重检查加锁
class Singleton {
// 加上 volatile 关键字
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;
}
}