What are the semantics of an AtomicReference
?
If I do:
AtomicReference<CustomObject> ref = new AtomicReference<CustomObject>();
and then I do:
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;
}
Is there a guarantee that on line obj = ref.get(); //????
the get
will return the most fresh version of CustomObject
?
This is related to the answer of assylias for post:
Actually, your code has a race condition which could cause new data to be lost:
ref.get()
and gets old dataref.set()
ref.set()
Since someMethod()
is always loading new data and you always want callers to wait while new data is loading, all this extra stuff is useless. just use a simple synchronized block (or Lock) around the whole block and ditch the atomic reference. according to the linked post, it seems like you only ever want to do this once, so use an initialized flag.
private boolean _initialized;
public synchronized void loadLatestData() {
if(!_initialized) {
// load latest data ...
_initialized = true;
}
}