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

- 31,165
- 11
- 75
- 105

- 13,167
- 13
- 66
- 88
6 Answers
Short answer is: No.
From the java.util.concurrent.atomic
package documentation. To quote:
The memory effects for accesses and updates of atomics generally follow the rules for volatiles:
get
has the memory effects of reading avolatile
variable.set
has the memory effects of writing (assigning) avolatile
variable.
By the way, that documentation is very good and everything is explained.
AtomicReference::lazySet
is a newer (Java 6+) operation introduced that has semantics unachievable through volatile
variables. See this post for more information.

- 303,325
- 100
- 852
- 1,154

- 12,614
- 4
- 38
- 46
-
16And the longer answer would be ? – Julien Grenier Nov 12 '08 at 05:01
-
Agreed. We at least need a link. – Julien Chastang Jan 30 '09 at 16:57
-
2The link to longer answer: http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/atomic/package-summary.html – Alex Siman Nov 02 '09 at 23:40
-
So what is writing a volatile without beeing Atomic like getAndSet() ? – user1767316 Oct 05 '21 at 13:18
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().

- 19,934
- 4
- 57
- 70
There are several differences and tradeoffs:
Using an
AtomicReference
get/set has the same JMM semantics as a volatile field(as the javadoc states), but theAtomicReference
is a wrapper around a reference, so any access to the field involves a further pointer chase.The memory footprint is multiplied (assuming a compressed OOPs environment, which is true for most VMs):
- volatile ref = 4b
AtomicReference
= 4b + 16b (12b object header + 4b ref field)
AtomicReference
offers a richer API than a volatile reference. You can regain the API for the volatile reference by using anAtomicFieldUpdater
, or with Java 9 aVarHandle
. You can also reach straight forsun.misc.Unsafe
if you like running with scissors.AtomicReference
itself is implemented usingUnsafe
.
So, when is it good to choose one over the other:
- Only need get/set? Stick with a volatile field, simplest solution and lowest overhead.
- Need the extra functionality? If this is a performance(speed/memory overhead) sensitive part of your code make a choice between
AtomicReference
/AtomicFieldUpdater
/Unsafe
where you tend to pay in readability and risk for your performance gain. If this is not a sensitive area just go forAtomicReference
. Library writers typically use a mix of these methods depending on targeted JDKs, expected API restrictions, memory constraints and so on.

- 2,841
- 22
- 27
JDK source code is one of the best ways to answers confusions like this. If you look at the code in AtomicReference, it uses a volatie variable for object storage.
private volatile V value;
So, obviously if you are going to just use get() and set() on AtomicReference it is like using a volatile variable. But as other readers commented, AtomicReference provides additional CAS semantics. So, first decide if you want CAS semantics or not, and if you do only then use AtomicReference.

- 23,928
- 8
- 94
- 159

- 3,316
- 4
- 26
- 33
-
17*"JDK source code is one of the best ways to answers confusions like this"* => I don't necessarily agree - the javadoc (which is the contract of the class) is the best way. What you find in the code answers the question for a specific implementation but code can change. – assylias Feb 03 '13 at 16:59
-
4For example [this variable](http://stackoverflow.com/a/14363501/829571) in hashmap was volatile in JDK 6 but is not volatile any longer in Java 7. Has you based your code on the fact that the variable was volatile, it would have broken when ugrading your JDK... Admittedly the example is different but you get the point. – assylias Feb 03 '13 at 17:00
-
-
1
AtomicReference
provides additional functionality which a plain volatile variable does not provide. As you have read the API Javadoc 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.

- 303,325
- 100
- 852
- 1,154

- 525,659
- 79
- 751
- 1,130
-
So the difference, then, is in their performance. If there was no difference, you wouldn't ever suggest using one over the other. – B T Feb 06 '12 at 21:07
-
The performance is much the same. An AtomicRefrence adds complexity and memory usage. – Peter Lawrey Feb 06 '12 at 21:09
-
@BT A `volatile` field can be used like any regular field whereas accessing the value in an `AtomicReference` requires going through `get` and `set` methods. – David Harkness Sep 03 '14 at 17:25
Sometimes even if you only use gets and sets, AtomicReference might be a good choice:
Example with volatile:
private volatile Status status;
...
public setNewStatus(Status newStatus){
status = newStatus;
}
public void doSomethingConditionally() {
if(status.isOk()){
System.out.println("Status is ok: " + status); // here status might not be OK anymore because in the meantime someone called setNewStatus(). setNewStatus should be synchronized
}
}
With AtomicReference it would be:
private AtomicReference<Status> statusWrapper;
...
public void doSomethingConditionally() {
Status status = statusWrapper.get();
if(status.isOk()){
System.out.println("Status is ok: " + status); // here even if in the meantime some called setNewStatus() we're still referring to the old one
}
}
One might say that you could still have a proper copy if you substituted:
Status status = statusWrapper.get();
with:
Status statusCopy = status;
However I guess the second one is more likely to be removed by someone accidentally in the future during "code cleaning".

- 647
- 1
- 7
- 22
-
Not sure why this was voted down. It does show a valid example where the value of `status` could change between the `if` test and its use in the `println` statement. I'm not sure which of the two versions is more likely to be removed during cleanup though. – LordOfThePigs Mar 27 '23 at 12:51