0
点赞
收藏
分享

微信扫一扫

单例应该这么写


public class DoubleCheckSingleton {

private volatile static DoubleCheckSingleton instance = null;

private DoubleCheckSingleton() {}

public static DoubleCheckSingleton getInstance() {
//先检查实例是否存在,不存在则进入下面的同步块
if (instance == null){
//同步块,线程安全的创建实例
synchronized (DoubleCheckSingleton.class) {
//再次检查是否存在实例,如果不存在,才真正创建实例
if (instance == null) {
instance = new DoubleCheckSingleton();
}
}
}
return instance;
}

}
public class DoubleCheckSingleton {

private volatile static DoubleCheckSingleton instance = null;

private DoubleCheckSingleton() {}

public static DoubleCheckSingleton getInstance() {
//先检查实例是否存在,不存在则进入下面的同步块
if (instance == null){
//同步块,线程安全的创建实例
synchronized (DoubleCheckSingleton.class) {
//再次检查是否存在实例,如果不存在,才真正创建实例
if (instance == null) {
instance = new DoubleCheckSingleton();
}
}
}
return instance;
}

}

 Q1. instance为什么要声明为static?

 A1. static方法只能访问static属性,因为非static属性是为实例而存在的,而调用static方法不需要实例存在,所以instance必须声明为static。

 Q2. getInstance方法里可以不加同步吗?

 A2. 不可以,必须加,因为很明显存在多线程问题

 Q3. 两次判断instance==null是必要的吗?

 A3. 是非常必要的,我们在很多框架如spring中也常看到这样的写法。因为同步是有性能降低代价的,所以同步前需判断null,这个不是必须,但很必要,同步块里的则是必须的判断了。

 Q4. instance为何声明为volatile?

 A4. 线程能够自动发现volatile变量的最新值,且instance的值只有非常确定的两种情况,即null和单实例,所以instance声明为volatile,才能以最新的值判断instance==null,这是必须的。关于volatile与内存模型的解释,请看​​http://wely.iteye.com/blog/2324588​​​ , ​​http://wely.iteye.com/blog/2228828​​

 Q5. 在调用方法时才new instance,这叫什么模式?

 A5. 懒汉模式,或可称为懒加载模式。

 

其实,单例还有一些深藏不漏的地方 -> 单例与序列化和反射

​​http://wely.iteye.com/blog/2230060​​

​​http://wely.iteye.com/blog/2228826​​

举报

相关推荐

0 条评论