What is the visibility problem in the Java Memory Model?

The visibility problem is a concurrency issue in the Java Memory Model where a write to a shared variable by one thread isn't guaranteed to be seen by another thread promptly, or at all, without proper synchronization. It stems from threads being allowed to cache variables locally, in registers or CPU caches, rather than reading and writing directly through main memory.

Key Points

Example

A background thread checking a plain boolean running flag in a tight while loop may never see it flip to false from another thread, because the JIT-optimized loop keeps reading a cached register value instead of checking main memory each iteration.

private volatile boolean running = true;

public void stop() {
    running = false; // visible to other threads immediately
}

Interview Tip

A concise interview answer is

The visibility problem happens when one thread's write to a shared variable isn't seen by another thread because it's reading a cached, stale value instead of going to main memory. Marking the variable volatile, or wrapping the access in synchronized, establishes a happens-before relationship that guarantees the update becomes visible.

Last updated: 16 July 2026

Previous question: What is the Java Memory Model?

Next question: How does garbage collection handle circular references?

More Memory Management interview questions

All Memory Management questions