volatile & the Java Memory Model
Part of the Java Roadmap
In short
One thread sets running = false, yet the other thread keeps looping forever. That is not a bug in your logic. It is the memory model, and volatile is the fix for this exact case.
The Java Memory Model defines when one thread's writes become visible to others. volatile guarantees visibility of a variable across threads but does not make compound actions like count++ atomic.

Think of it like this
Personal notebooks vs the notice board
Each clerk in a government office copies the rules into a personal notebook and reads from that. When the boss changes a rule, clerks may keep following their old copy for a while. A volatile variable is a rule pinned on the shared notice board: everyone must read it from the board every time, so a change is seen at once.
Watch it happen
Why the stop flag gets missed
1/4The worker thread reads running = true. Without volatile, the JIT may keep that value in a register and never read it again.
The big ideas
What you need to know
Visibility
The JMM only promises a thread sees another's write when there is a happens-before link, such as a lock, volatile or Thread.join.
volatile = always fresh
Reads and writes of a volatile field go to shared memory and are not reordered around it. Perfect for flags.
Not atomic
volatile int count; count++ is still three steps and still races. Use AtomicInteger or a lock for that.
Happens-before
Everything a thread did before writing a volatile is visible to a thread that later reads that volatile.
See it in code
Walk through the code
1public class Worker implements Runnable {2 private volatile boolean running = true;3 4 @Override5 public void run() {6 while (running) {7 processNextMessage();8 }9 System.out.println("Worker stopped cleanly");10 }11 12 public void stop() {13 running = false;14 }15 16 private void processNextMessage() { /* ... */ }17}18 19// elsewhere20Worker w = new Worker();21Thread t = new Thread(w);22t.start();23// ... later24w.stop();25t.join();1/4volatile makes every read of running see the latest write from any thread.
A classic stop flag. Remove volatile and, on some JVMs, the loop can spin forever after stop() is called.
Save yourself the pain
Common mistakes
Oops
Making a counter volatile and calling count++ from many threads.
Do this instead
volatile does not make read-modify-write atomic. Use AtomicInteger or synchronized.
Oops
Assuming a write in one thread is instantly seen by others.
Do this instead
Without a happens-before link (volatile, locks, join, concurrent collections) there is no such guarantee.
Oops
Adding volatile to every field 'to be safe'.
Do this instead
Use it for simple flags and published references. For compound state, use locks or immutable objects.
Your mission
See the invisible write
Quick check
Which statement about volatile is true?
Go deeper
- 1Use a volatile boolean flag to stop a running thread.
- 2Explain why volatile alone doesn't fix a counter race condition.
- 3Read about the 'happens-before' relationship.
More topics at this stop
Stuck on this topic? Ask an Insider
Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.