Atomic variables are described as "better volatiles" in Java Concurrency in Practice (see section 15.3). Here is an extract from this book:
The atomic variable classes provide a generalization of volatile
variables to support atomic conditional read-modify-write operations.
AtomicInteger
represents an int
value, and provides get
and set
methods with the same memory semantics as reads and writes to a
volatile int
.
Applied to your case this means that if you're using only get()
and set()
methods of AtomicBoolean
, they can be safely replaced with read-writes to volatile boolean
.
volatile
is needed to guarantee that all threads will see up-to-date value of the variable. Back to Java Concurrency in Practice (section 3.1.4):
When a field is declared volatile
, the compiler and runtime are put on
notice that this variable is shared and that operations on it should
not be reordered with other memory operations. Volatile
variables are
not cached in registers or in caches where they are hidden from other
processors, so a read of a volatile
variable always returns the most
recent write by any thread.