When to use volatile?
On the previous pages, we've looked at the definition of volatile
and seen some of the additional atomic access to volatile variables
provided by Java 5. We've also seen that volatile variables share some features (in particular the
memory synchronization behaviour) of synchronized access to an object.
So when should you use volatile?
First, the easy cases where you basically don't need volatile or any other
- volatile is not necessary– or in fact possible–
for fields that are immutable (declared final);
- volatile is not necessary for variables that are accessed
by only one thread (though of course you have to make a correct
decision that they are only accessed by one thread!);
- volatile is not suitable for complex operations
where you need to prevent access to a variable for the duration of the operation:
in such cases, you should use object synchronization
or one of Java 5's explicit lock classes added.
Now some typical uses.
A "simple flag" accessed by multiple threads
The most typical case is where:
- you write a variable, such as a flag, in one thread;
- you check that variable in another thread;
- crucially, the value to write doesn't depend on the
- ...or, you don't care about "missing an update".
We looked at the example of a volatile flag to control a loop.
The last point above outlaws cases such as x++ or x += 7,
because this actually involves
reading the current value, incrementing it, then setting the new value. A common bug with volatile is to assume that x++ is atomic.
For cases where you need this functionality, consider AtomicInteger
or related classes.
Now, under some circumstances, you could make the decision to perform
concurrent x++ operations anyway and "take the risk" that occasionally you'll miss
A field of a class that you'll instantiate a large number of times
In general, where you need atomic access to a "one-off" variable or one created
a fairly small number of times, then the Java 5 atomic classes
(primarily AtomicInteger) are your answer.
But if you're creating a large number of instances of an object containing a
field that needs atomic access, using a volatile field and accessing it via an
(or AtomicIntegerFieldUpdater or AtomicLongFieldUpdater for primitive fields)
will generally be more efficient.
Avoiding messy syntax
As discussed in our section on AtomicReferenceFieldUpdater, using a votatile variable with a field updater as opposed to one of
the atomic wrapper classes allows you to perform "normal" read and writes to the variable using
regular Java syntax. The disadvantage of AtomicInteger, AtomicReference etc is
that every access– even if just a single read or write– must go via one of
the method calls. (On the other hand, this forces you to think about what's going on and may
avoid bugs of the volatileVariable++ kind.)
If you enjoy this Java programming article, please share with friends and colleagues. Follow the author on Twitter for the latest news and rants.
Editorial page content written by Neil Coffey. Copyright © Javamex UK 2021. All rights reserved.