"What does Java's volatile keyword guarantee, and what does it not?"
volatile is the most misunderstood Java keyword. Interviewers use it to test whether you understand the Java Memory Model — many candidates think volatile means "atomic", which is wrong and leads to production bugs.
Two guarantees: (1) visibility — a write to a volatile variable is immediately visible to all other threads' reads; (2) ordering — the JVM won't reorder volatile writes/reads with surrounding memory operations in ways that violate happens-before.
What volatile does NOT provide: atomicity for compound operations. `volatile int i; i++;` is still not thread-safe because i++ is read-modify-write.
Implementation: volatile writes emit a StoreLoad barrier on x86 (lock prefix), forcing the write to drain from the store buffer to main memory and invalidating other cores' cached copies.
Classic use case: the DCL (double-checked locking) singleton pattern — instance must be volatile to prevent the partial-construction publishing issue where another thread sees a non-null reference to a half-initialized object.
When to use AtomicXxx instead: anything involving compound updates (increment, check-and-set). AtomicInteger.incrementAndGet() uses CAS internally to make the operation atomic.
volatile gives you two guarantees. First, visibility — a write to a volatile variable is immediately visible to reads from other threads. Without volatile, one thread can write a value and another thread might see a stale value forever because the write sits in the writing thread's store buffer or local cache. volatile forces the write to drain to main memory and invalidates other cores' cached copies. Second, ordering — volatile imposes happens-before ordering around the access. The JVM and CPU won't reorder earlier writes to non-volatile fields past a volatile write, or later reads of non-volatile fields before a volatile read. This is crucial for publishing objects safely. What volatile does NOT give you is atomicity for compound operations. volatile int i; i++ is still a bug — i++ is really read, add 1, write, and another thread can read the same value between the read and write. Use AtomicInteger for that; incrementAndGet() uses CAS to make the whole operation atomic. Implementation-wise, on x86 the JVM implements volatile writes with a lock-prefixed instruction that acts as a memory barrier (StoreLoad) — it drains the store buffer and forces a cache coherence cycle. Volatile reads on x86 are essentially free because x86's memory model is already strong enough. On weaker architectures like ARM the barriers matter more and are more expensive. Classic use case: double-checked locking singleton. instance must be volatile. Without it, thread A can see instance as non-null while the object's constructor hasn't finished — because the publishing write of "instance = new Foo()" can be reordered so the reference becomes visible before the fields are initialized. volatile prevents that reordering. When to use volatile vs AtomicXxx vs synchronized: volatile for simple flag / state — "is shutdown requested?" is a classic single-writer, many-reader case. AtomicXxx for counters and compound updates. synchronized for multi-variable invariants or guarded blocks where several fields must move together.
Always explicitly say volatile is NOT atomic — interviewers love to catch the i++ misconception.
DCL singleton with volatile is a classic follow-up — memorize the pattern.
Volatile → memory barrier → x86 lock prefix is the right depth for senior interviews.
If you blank on "happens-before" in the interview, HireMe AI can surface the JMM term.
Single-writer / many-readers flags or state variables; publishing a fully-initialized object reference; cross-thread shutdown signals.
Yes, much faster on x86 — essentially free. But they do different things; synchronized gives atomicity, volatile doesn't.
Yes. The JMM requires that writes to volatile long/double are atomic (no word tearing), regardless of platform.
HireMe AI listens to your interview in real-time, detects questions, and generates instant answer suggestions — so you never freeze up again.
Try HireMe AI Free