的语义是AtomicReference
什么?
如果我做:
AtomicReference<CustomObject> ref = new AtomicReference<CustomObject>();
然后我做:
public void someMethod(){
//do something
ref.set(loadNewData());
}
private final Sempahore updatePermit = new Sempahore(1);
private CustomObject loadNewData(){
CustomObject obj = null;
if (updatePermit.tryAcquire()) {
obj = ...; //do the loading and create Object
updatePermit.release();
} else {
//update already running, wait
updatePermit.acquire();
//release the permit immediately
updatePermit.release();
obj = ref.get(); //????
}
return obj;
}
是否可以保证在线obj = ref.get(); //????
将get
返回最新版本的CustomObject
?
这与 assylias 对post的回答有关: