Java Concurrency: AtomicReference
Java Concurrency: AtomicReference
Want to learn more about concurrency in Java?
Join the DZone community and get the full member experience.Join For Free
Java.util.concurrent.atomic.AtomicReference is a class designed to update variables in a thread-safe way. Why do we need the class
AtomicReference? Why can we not simply use a volatile variable? And how can we use it correctly?
For the tool I am writing, I need to detect if an object was called from multiple threads. I use the following immutable class for this:
You can download the source code of all examples on GitHub.
I store the first thread accessing an object in the variable thread, line 2. When another thread accesses the object, I set the variable
accessedByMultipleThreads to true and the variable thread to null, line 23. When the variable
accessedByMultipleThreads is true, I do not change the state, line 15 until 17.
I use this class in every object to detect if it was accessed by multiple threads. The following example uses the state in the class
I store the state in the volatile variable state, line 2. I need the volatile keyword to make sure that the threads always see the current values, as explained in greater detail here.
To check if using a volatile variable is thread-safe, I use the following test:
I need two threads to test if using a volatile variable is thread-safe, created in line 9 and 10. I start those two threads, line 11 and 12. And then wait till both are ended using thread join, line 13 and 14. After both threads are stopped I check if the flag accessedByMultipleThreads is true, line 15.
To test all thread interleavings we put the complete test in a while loop iterating over all thread interleavings using the class AllInterleavings from vmlens, line 7. Running the test I see the following error:
The vmlens report shows what went wrong:
The problem is that for a specific thread interleaving both threads first read the state. So, one thread overwrites the result of the other thread.
How to Use AtomicReference?
To solve this race condition, I use the
compareAndSet method from
compareAndSet method takes two parameters, the expected current value and the new value. The method atomically checks if the current value equals the expected value. If yes, then the method updates the value to the new value and returns true. If not, the method leaves the current value unchanged and returns false.
The idea to use this method is to let
compareAndSet check if the current value was changed by another thread while we calculated the new value. If not, we can safely update the current value. Otherwise, we need to recalculate the new value with the changed current value.
The following shows how to use the
compareAndSet method to atomically update the state:
I now use an
AtomicReference for the state, line 2. To update the state, I first need to get the current value, line 5. Then, I calculate the new value, line 6, and try to update the
compareAndSet, line 7. If the update succeeds, I am done. If not, I need to get the current value again, line 8, and recalculate the new value, line 9. Then, I can try again to update the
compareAndSet. I need a while loop since the
compareAndSet might fail multiple times.
As Grzegorz Borczuch pointed out in a comment to this article there is since JDK 1.8 an easier to use method in AtomicReference which achieves the same result: updateAndGet. This method internally uses compareAndSet using a while loop to
update the AtomicReference.
Using volatile variables leads to race conditions since specific thread interleavings for a thread overwrites the computation of other threads. By using the
compareAndSet method from the class
AtomicReference, we can circumvent this race condition. We atomically check if the current value is still the same as when we started the computation. If yes, we can safely update the current value. Otherwise, we need to recalculate the new value with the changed current value.
Published at DZone with permission of Thomas Krieger , DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.