单例模式属于创建型模式,主要用于解决频繁创建和销毁全局使用的类实例的问题。
注意:
- 1、单例类只能有一个实例。
- 2、单例类必须自己创建自己的唯一实例。
- 3、单例类必须给所有其他对象提供这一实例。
按照实例化时机可分为 饿汉式 和 懒汉式 两种
饿汉式
在类加载的时候实例化对象
public class Singleton {
private static Singleton instance = new Singleton();
private Singleton(){}
public static Singleton getInstance(){
return instance;
}
}
懒汉式
第一次需要用到的时候实例化对象,有两种写法
第一种:给 getInstance 方法加锁,代码编写简单,但是效率不高
public class Singleton {
private static Singleton instance;
private Singleton(){}
public synchronized static Singleton getInstance(){
if(instance == null){
instance = new Singleton();
}
return instance;
}
}
第二种:使用双检锁,现实中使用较多
public 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;
}
}
这里给instance变量加上volatile关键字是为了防止代码重排序
instance = new Singleton(); 这一行代码可拆解为 3 步
- 分配内存
- 初始化对象
- 指向刚分配的地址
如果发生了代码重排序,可能流程变成 1 -> 3 -> 2
这样可能出现一种情况
线程 A 按照 1 -> 3 -> 2的流程执行,先让instance指向了一个地址
此时线程 B 进入这个方法,直接获取了 instance,但此时instance 尚未初始化
所以我们利用 volatile 关键字防止代码重排序。