Medium👤 3-5 years👤 8-15 years 1 min read

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

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.

Coffee Chat Question

Concept Made Simple

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

🧠Mind Map Answer

Remember It Faster

Guaranteesvisibility + ordering (happens-before)
Does NOTmake count++ atomic
Use forflags (running), safe publication

⌨️Hands-on Keyboard

Learn by Doing

java
private volatile boolean running = true; // visible to all threads
public void stop() { running = false; }
public void loop() { while (running) { /* ... */ } }

🔥What If?

Think Beyond the Expected

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 + orderingnot atomic for compound opshappens-beforestop-flag use caseAtomic 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

  • 1Why isn't volatile enough for a counter?
  • 2What is the happens-before relationship?
  • 3How does volatile enable safe publication?

🧩Related Technologies

AtomicIntegerjava.util.concurrentJava Memory Model

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?

Support our platform by exploring our recommended products.

As an Amazon affiliate, purchases through these links may earn us a small commission — at no extra cost to you. It helps keep Full Stack Interview Guru free.

Related Questions