Simplest and understandable example of volatile keyword in Java
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
The simplest way to understand volatile in Java is to think of it as a visibility guarantee between threads. When one thread changes a volatile variable, other threads are required to see the updated value instead of continuing to use a stale cached copy.
The Classic Stop-Flag Example
Here is the most common example:
Without volatile, the worker thread might keep looping because it does not reliably observe that running changed to false.
With volatile, the change made by the main thread becomes visible to the worker.
What volatile Guarantees
For a variable marked volatile, Java guarantees:
- reads see the most recently written value
- writes are visible across threads
- certain reordering around that variable is constrained
That makes volatile a good fit for:
- stop flags
- ready flags
- state indicators where one write is read by other threads
It is about visibility, not about turning all thread interactions into safe atomic transactions.
What volatile Does Not Guarantee
This is the part people miss. volatile does not make compound actions atomic.
For example:
You might expect 20000, but counter++ is not a single atomic action. It is a read, modify, write sequence, and two threads can still interfere with each other.
For atomic increments, use AtomicInteger or synchronization.
When to Use AtomicInteger Instead
This is the right tool when the operation itself must be atomic, not just visible.
Common Pitfalls
The most common mistake is believing volatile makes counter++ thread-safe. It does not. It only ensures that threads see updated values, not that compound updates happen safely.
Another issue is using volatile where multiple variables must stay consistent with each other. Visibility of a single field is not enough if correctness depends on coordinated updates across several fields.
A third pitfall is avoiding synchronization entirely just because volatile seems lighter. Sometimes you really need a lock or an atomic class because correctness matters more than minimal syntax.
Finally, do not use volatile as a magic "thread-safe" label. It solves a very specific visibility problem and should be chosen for that reason.
Summary
- '
volatilein Java guarantees visibility of writes across threads.' - The simplest example is a shared stop flag read by one thread and written by another.
- '
volatiledoes not make compound operations such as++atomic.' - Use
AtomicIntegeror synchronization when an update itself must be thread-safe. - Think of
volatileas a visibility tool, not a full concurrency solution.
Related reading
- Simplest async/await example possible in Python
- Simplest async/await example possible in Python
- Simplest way to do a fire and forget method in C?
- Simplest way to do a fire and forget method in c 4.0
- Simplest way to read JSON from a URL in Java
- SimpMessagingTemplate.convertAndSend with RabbitMQ works very slow
- Single thread concept of JavaScript running in browser
- Singleton/Synchronization in Clustered environment

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.