//双重校验锁实现单例
public class synchronizedDemo {
//volatile 禁止jvm重排 保证内存的可见性
private volatile static synchronizedDemo uniqueIntance;
//private 构造函数私有化
private synchronizedDemo(){
}
private static synchronizedDemo getUniqueIntance(){
//锁外判断,避免每次进来都获取锁
if(null == uniqueIntance) {
synchronized (synchronizedDemo.class) {//锁内判断,避免多线程产生重复对象
if (null == uniqueIntance) {
uniqueIntance = new synchronizedDemo();
}
}
}
return uniqueIntance;
}
}