单例模式的双重检测问题
程序员文章站
2022-07-14 09:55:56
...
单例模式分为懒汉式和饿汉式两种,一种是以时间换空间,一种则是以空间换时间,而且饿汉式是具有线程安全,就不必过多讨论。我们组要讨论为什么饿汉式要进行双重检测??它又有什么问题??
新手可能写出下面的代码
private Singleton() { } // 默认构造器
private static Singleton instance = null;// 延时加载
//每次运行都要创建实例因而需要加锁保护
public static synchronized Singleton getIntance() {
if (instance == null) {
instance = new Singleton();// 判断之后加载
}
return instance;
}
以上可以保证线程安全,但每次都需要获取锁,承受同步带来的性.能开销.所以我们不需要每次都获取锁,只是在创建实例的时候需要而已,下面的代码再加一重检测,每次调用函数时再第二次检测时才考虑取锁,避免了同步开销。
public class Singleton {
private static Singleton instance = null;
private Singleton() {}
public static Singleton getInstance() {
if (instance == null) {
synchronized (Singleton.class) {// 1
if (instance == null) {// 2
instance = new Singleton();// 3
}
}
}
return instance;
}
}
上面的代码还是有些小问题,就是instance = new Singleton()可能出现重排序.如下,我们默认的是
inst = allocat(); // 分配内存
constructor(inst); // 先执行构造函数
instance = inst; // 后赋值
但虚拟机可能导致
inst = allocat(); // 分配内存
instance = inst; // 先赋值
constructor(inst); // 后执行构造函数
若是第二种,其他线程访问时,instance不为null但构造函数没有完成而导致程序崩溃.所以上面的程序需要加上避免重排的发生,这时就引入了 volatile 关键字。
private volatile static Singleton instance = null;
参考:
http://jiangzhengjun.iteye.com/blog/652440
http://www.jianshu.com/p/5b2f063d9f68
上一篇: 单例模式的双重检测