The Java Memory Model (JMM) defines how threads interact with memory and establishes the rules for reading and writing shared variables in a multithreaded environment. It ensures that changes made by one thread become visible to other threads in a predictable and consistent manner.
Why Do We Need the Java Memory Model?
For performance reasons, threads may work with cached copies of variables instead of reading directly from main memory.
Thread 1 updates a variable.
Thread 2 may continue reading an outdated cached value.
Inconsistent application behavior.
The Java Memory Model defines rules that ensure threads see data correctly.
How JMM Is Linked to Threads
Each thread has its own working memory where copies of variables may be stored.
Main Memory
|
---------------------
| | |
Thread A Thread B Thread C
Working Working Working
Memory Memory Memory
JMM defines how and when these changes become visible.
Three Important Concepts of JMM
1. Visibility
Visibility ensures that changes made by one thread are visible to other threads.
flag = true;
while (!flag) {
}Thread 2 may never see the updated value.
Use volatile or synchronization.
private volatile boolean flag =
false;Now all threads see the latest value.
2. Atomicity
Atomicity means an operation is performed completely or not at all.
count++;
This is not atomic.
1. Read count
2. Increment value
3. Write count
Multiple threads can interfere between these steps.
3. Ordering
The JVM and CPU may reorder instructions for optimization.
x = 10;
flag = true;The JVM may internally reorder instructions.
flag = true
before
x = 10
The Happens-Before Relationship
One of the most important concepts in JMM is Happens-Before.
If Action A happens-before Action B,
then the effects of A are visible to B.
Role of volatile in JMM
private volatile boolean running =
true;running = false;
Other threads immediately observe the update.
volatile does not provide atomicity.
Role of synchronized in JMM
public synchronized void update() {
value++;
}Suppose a banking application has a shared account balance.
Deposits money.
Checks balance.
Thread 2 may see stale data.
Thread 2 always sees the latest balance.
class SharedData {
private volatile boolean ready =
false;
public void setReady() {
ready = true;
}
public boolean isReady() {
return ready;
}
}
public class Demo {
public static void main(String[] args) {
SharedData data =
new SharedData();
Thread writer =
new Thread(() -> {
data.setReady();
});
Thread reader =
new Thread(() -> {
while (!data.isReady()) {
}
System.out.println(
"Data Available");
});
writer.start();
reader.start();
}
}Data Available
Without volatile, the reader thread might not observe the update immediately.
Real-World Example
Consider a web server.
Updates application configuration.
Reads configuration values.
Benefits of JMM
A concise interview answer is
The Java Memory Model (JMM) defines how threads interact with shared memory and establishes rules for visibility, atomicity, and ordering of operations. It ensures that changes made by one thread become visible to other threads in a predictable manner. Features such as synchronized, volatile, and locks rely on the JMM to provide safe communication between threads.
Last updated: 11 November 2026
Previous question: What challenges might you face with multithreaded programs in Java?
Next question: Can we create a server in Java without Spring or any other framework?