views:

1828

answers:

3

Is there any difference between a volatile Object reference and AtomicReference in case I would just use get() and set()-methods from AtomicReference?

+7  A: 

Short answer is No:

From the java.util.concurrent.atomic package doc:

The memory effects for accesses and updates of atomics generally follow the rules for volatiles:

* get has the memory effects of reading a volatile variable.
* set has the memory effects of writing (assigning) a volatile variable.

BTW the doc for the package is very good and everything is explained...

-Patrick

pgras
And the longer answer would be ?
Julien Grenier
Agreed. We at least need a link.
Julien Chastang
The link to longer answer: http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/atomic/package-summary.html
Alex Siman
+5  A: 

No, there is not.

The additional power provided by AtomicReference is the compareAndSet() method and friends. If you do not need those methods, a volatile reference provides the same semantics as AtomicReference.set() and .get().

Avi
A: 

AtomicReference provides additional functionality which a plain volatile variable does not provide. As you have read API you will know this, but it also provides a lock which can be useful for some operations.

However, unless you need this additional functionality I suggest you use a plain volatile field.

Peter Lawrey