MediumExperience level: 3-5 years · 8-15 years1 min read

What does the volatile keyword guarantee, and what does it NOT?

Reviewed by Gurusankar M.

Asked inAmazonMicrosoftDeloitte
#volatile#visibility#happens-before#memory model#atomicity
Report issue

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

java
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

  1. Why isn't volatile enough for a counter?
  2. What is the happens-before relationship?
  3. 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.
Open inChatGPTGeminiClaude

Was this answer helpful?

Affiliate links

Equipment for a comfortable, productive setup.

As an Amazon Associate I earn from qualifying purchases.