Java单例模式 以原子操作方式的实现

[b]问题[/b]
[color=red]不同Java虚拟机实现产生有引用未构造问题[/color]
Java虚拟机初始化对象时,分析如下
SomeObject so = new SomeObject();
Java虚拟机JVM
指令
so ====> memory A步骤
.
. =====> construct--执行构造 B步骤
.
. =====> asign -----指定其他的引用,或者实例化so中的其他变量 C步骤
. .
. . 其他步骤
. .
当一个线程执行到 步骤A 时,另一个线程访问 so对象
如 SomeObject getSo()
此时 getSo() 返回 so对象的引用不为空
我们拿着一个so对象引用去获取so对象中的另一个属性,比如so.getOtherAttribute()
这个时候这个OtherAttribute在构造器中或其他步骤中,并没有执行完成,这将
会导致一个运行时异常。
我用一个单例模式的实现来说明此类问题的[color=blue]解决方法[/color]

package test;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;

public class AtomicSingleton {

private static AtomicReference<AtomicSingleton> atomicReference = new AtomicReference<AtomicSingleton>();

//test how constructor get into by new AtomicSingleton statement
private AtomicInteger atomicInteger = new AtomicInteger(0);

private AtomicSingleton(){
int i = atomicInteger.get();
atomicInteger.addAndGet(i++);
}

public static AtomicSingleton getInstance(){
AtomicSingleton atomicSingleton = atomicReference.get();
if(null == atomicSingleton){
synchronized (AtomicSingleton.class) {
//double checking the AtomicSingleton whether initial, ensure not new AtomicSingleton twice
//to waste the system resource
if(null!=(atomicSingleton=atomicReference.get())){
return atomicSingleton;
}

//ensure the locate the memory, construct and other instructs is atomic,
//whatever the implementation of the JVM
atomicReference.getAndSet(new AtomicSingleton());
//atomicReference.compareAndSet(null, new AtomicSingleton());
atomicSingleton = atomicReference.get();
}
}
return atomicSingleton;
}

}


对于多线程编程时,我们可以考虑使用java.util.concurrent.atomic.*包下的一系列
确保原子操作对象来实现原子操作,以排除不同Java虚拟机实现会产生的上述问题。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值