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 synchronization mechanism:

Now some typical uses.

A "simple flag" accessed by multiple threads

The most typical case is where:

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 an update.

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 AtomicReferenceFieldUpdater (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.