Volatile boolean vs AtomicBoolean
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
In the world of concurrent programming in Java, ensuring thread-safety and memory visibility between threads is paramount. Two common constructs that aid in this are volatile boolean variables and AtomicBoolean. Both are used under circumstances where multiple threads need to access and modify a boolean variable, but they cater to different needs and have various implications on performance and usage.
Understanding volatile
The volatile keyword in Java is used to indicate that a variable's value will be modified by different threads. Declaring a variable as volatile ensures that the value of the variable is always read from the main memory and not from the thread's local cache. This guarantees memory visibility, i.e., changes made in one thread are visible to other threads immediately.
Here is a simple example:
In this example, any update to flag in the writerThread is immediately visible to readerThread.
Understanding AtomicBoolean
AtomicBoolean, part of java.util.concurrent.atomic package, provides a way for a boolean value to be read and written atomically. Unlike volatile, AtomicBoolean not only ensures visibility but also provides atomicity for compound actions like compare-and-set.
Example usage:
This snippet is functionally similar to the volatile example but with the addition of atomic operations that AtomicBoolean supports, like:
This method sets flag to true only if it's currently false and does so atomically, which ensures that no other thread can interfere between the "check" and "set" actions.
Comparing volatile boolean and AtomicBoolean
Here’s a summarization of when to use volatile boolean versus AtomicBoolean:
| Feature | volatile boolean | AtomicBoolean |
| Basic Use | Ensures memory visibility. Useful in simple flag scenarios where the concern is to reflect a signal (e.g., stop a thread). | Provides methods for atomic operations, useful in cases where complex atomicity is crucial (e.g., compare-and-set). |
| Performance | Generally faster for simple read/write operations. | Slightly slower due to more complex atomic operations, but necessary for compound actions. |
| Use Case | Simple turn-on or turn-off signals. | Counters, toggles, and other scenarios where the state depends on previous state. |
Additional Details
Thread Safety with volatile
Although volatile is helpful in visibility concerns, it does not provide atomicity for compound actions. For instance, incrementing a volatile int is not thread-safe because incrementation involves multiple steps (read-modify-write).
Atomic Classes and AtomicBoolean
The atomic classes, including AtomicBoolean, are part of the Java concurrency package, which provides a higher level of concurrency control through lock-free, thread-safe operations based on CAS (Compare-And-Swap) algorithms. This makes AtomicBoolean a critical component when handling more complex scenarios involving boolean states.
Conclusion
Choosing between volatile boolean and AtomicBoolean depends largely on the specific requirements of your application. If you’re only looking to ensure that changes to a boolean variable are consistently visible across threads, and your application structure allows for simple assign-and-check operations, volatile is adequate and more performant. However, if you need to perform atomic operations on the boolean variable, particularly when these operations depend on the current state, AtomicBoolean is necessary to maintain thread safety and consistency.
Related reading

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.