What does the volatile keyword guarantee, and what does it NOT?
Reviewed by Gurusankar M.
Short Answer
volatile guarantees visibility (every read sees the latest write) and ordering (no reordering across the access) via happens-before. It does NOT make compound operations like count++ atomic — for that use Atomic classes or locks.
On this page
Concept Made Simple
Coffee Chat Question
“What does the volatile keyword guarantee, and what does it NOT?”
Remember It Faster
Mind Map Answer
- Guarantees
- visibility + ordering (happens-before)
- Does NOT
- make count++ atomic
- Use for
- flags (running), safe publication
Learn by Doing
Hands-on Keyboard
private volatile boolean running = true; // visible to all threads
public void stop() { running = false; }
public void loop() { while (running) { /* ... */ } }Think Beyond the Expected
What If?
A worker loop never stops after you flip a non-volatile boolean from another thread — why?
Without volatile, the JVM/JIT may cache the flag in a register, so the worker never sees the update — it loops forever. Marking the flag volatile forces a fresh read from main memory each iteration.
Real World
The volatile stop-flag is the canonical fix for 'my background thread won't shut down'; misusing volatile for counters (count++) is a classic lost-update bug.
Interviewer's Expectation
Keywords they're listening for:
- visibility + ordering
- not atomic for compound ops
- happens-before
- stop-flag use case
- Atomic for increments
Common Mistakes
- Using volatile for count++ (not atomic)
- Assuming volatile is a substitute for locking
- Forgetting volatile on a cross-thread flag
Best Practices
- volatile for flags / single-writer publication
- Atomic*/locks for read-modify-write
- Reason in terms of happens-before
Follow-up Questions
- Why isn't volatile enough for a counter?
- What is the happens-before relationship?
- How does volatile enable safe publication?
Continue Learning with AI
Take this question deeper with your favourite AI assistant. Pick a depth, copy the prompt, or open it directly — AI is your learning companion, not a shortcut.
Plain-language foundations
I'm preparing for a software engineering interview and want to understand this from scratch, as a beginner. Topic: volatile & Memory Model (Multithreading) Interview question: "What does the volatile keyword guarantee, and what does it NOT?" Please: 1. Explain the core idea in simple, plain language, using an everyday analogy. 2. Define any technical terms you use. 3. Walk through one small, concrete example. 4. Finish with a single sentence I can easily remember. Keep the tone friendly and assume I'm new to this topic.
Was this answer helpful?
Affiliate links
Tools for Your Developer Workspace
Equipment for a comfortable, productive setup.
As an Amazon Associate I earn from qualifying purchases.
- MonitorView on Amazon : BenQ EW2790U 27-inch Monitor (opens in a new tab)
BenQ EW2790U 27-inch Monitor
- LaptopView on Amazon : Acer Aspire One Laptop (opens in a new tab)
Acer Aspire One Laptop
- Office ChairView on Amazon : Wakefit Gravita Ergonomic Office Chair (opens in a new tab)
Wakefit Gravita Ergonomic Office Chair