What is the Java Memory Model, and how is it related to threads?

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.

Key Points

Why Do We Need the Java Memory Model?

Modern computers use

For performance reasons, threads may work with cached copies of variables instead of reading directly from main memory.

Problem

Thread 1 updates a variable.

Thread 2 may continue reading an outdated cached value.

Result

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.

Structure

Main Memory

|

---------------------

| | |

Thread A Thread B Thread C

Working Working Working

Memory Memory Memory

When a thread updates a shared variable

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.

Example

Thread 1

flag = true;

Thread 2

while (!flag) {

}

Without proper synchronization

Thread 2 may never see the updated value.

Solution

Use volatile or synchronization.

Example

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.

Example

count++;

This is not atomic.

Internally

1. Read count

2. Increment value

3. Write count

Multiple threads can interfere between these steps.

Solution

3. Ordering

The JVM and CPU may reorder instructions for optimization.

Example

x = 10;
flag = true;

The JVM may internally reorder instructions.

Without proper memory guarantees

Another thread may see

flag = true

before

x = 10

JMM prevents harmful reordering using

The Happens-Before Relationship

One of the most important concepts in JMM is Happens-Before.

It guarantees

If Action A happens-before Action B,

then the effects of A are visible to B.

Examples

Role of volatile in JMM

volatile ensures

Example

private volatile boolean running =
        true;

If one thread changes

running = false;

Other threads immediately observe the update.

Important

volatile does not provide atomicity.

Role of synchronized in JMM

synchronized provides

Example

public synchronized void update() {

    value++;
}

Benefits

Example

Suppose a banking application has a shared account balance.

Thread 1

Deposits money.

Thread 2

Checks balance.

Without JMM guarantees

Thread 2 may see stale data.

With synchronization

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();
    }
}

Output

Data Available

Without volatile, the reader thread might not observe the update immediately.

Real-World Example

Consider a web server.

Thread 1

Updates application configuration.

Thread 2

Reads configuration values.

The Java Memory Model ensures

Benefits of JMM

Interview Tip

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?

More Core Java - Level II interview questions

All Core Java - Level II questions