Reputation: 19552
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:
Upvotes: 2
Views: 3051
Reputation: 53694
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;
}
}
Upvotes: 3
Reputation: 122364
Atomic variables (including AtomicReference
) have the same properties as volatile
fields, in that they establish a happens-before relation across threads. If one thread A writes a value to a volatile field (or atomic variable) and another thread B reads from that same variable then you can't necessarily guarantee whether B will or will not see A's change, but you can guarantee that
Upvotes: 1
Reputation: 340713
Reading JavaDoc of java.util.concurrent.atomic
:
get
has the memory effects of reading avolatile
variable.
set
has the memory effects of writing (assigning) avolatile
variable.
So the guarantees are: get()
always returns the last value passed to set()
. It doesn't matter what you do in the meantime, what kind of locks you use, etc. Modifying volatile
variable (which you effectively do) is guaranteed to be visible by all other threads reading that value.
Upvotes: 3
Reputation: 887285
Yes.
AtomicReference
guarantees publication.
However, there is no guarantee that a different thread won't set it a millisecond later.
Note that if you aren't calling compareAndSet()
, an AtomicReference
is no better than a volatile
field.
Upvotes: 3