Best Division

Spread Your Knowledge

  • All topics
  • About us
  • Contact us
  • Privacy policy
  • Terms of service
  • Copyright
  • Facebook
  • Twitter
  • Instagram
  • Youtube
  • LinkedIn
  • Pinterest
  • Github
  • Tumblr

© 2026 Best Division

What Is the Java Memory Model (JMM)? Explained

Java
Jayant Kumar
Jayant Kumar@jayantkumar314
PublishedOct 15, 2024
UpdatedNov 16, 2026
6 min read

The Java Memory Model (JMM) is the part of the Java Language Specification (chapter 17) that defines when a write by one thread is guaranteed to be visible to another and which reorderings the compiler, JIT and CPU may perform. It gives you rules, mainly the happens-before relation, so you can write correct multithreaded code without depending on one machine's behaviour.

Without these rules a thread may read a stale value forever, or see operations in a different order than the source code suggests. The JMM tells you which tools, such as volatile, synchronized, locks and final fields, create the guarantees you need.

Table of Contents

  • Why a memory model is needed
  • The happens-before relation
  • Visibility with volatile
  • Atomicity and mutual exclusion with synchronized
  • Data races and what they permit
  • final fields and safe publication
  • Double-checked locking done right
  • Frequently Asked Questions
    • Is volatile enough for thread safety?
    • Is the Java Memory Model the same as the JVM heap layout?
    • Does the memory model change with virtual threads?
    • How can I test for data races?
  • Summary

Why a memory model is needed

Modern CPUs keep values in per-core caches and registers, and compilers reorder instructions to go faster. Within one thread this is invisible, because the result must match sequential execution. Across threads it is not: another thread may observe writes late, or in a different order.

java

If work() runs in one thread and stop() in another, the JMM does not require the worker to ever see running = false. The JIT may even hoist the read out of the loop, creating an infinite loop.

The happens-before relation

If action A happens-before action B, then A's effects are visible to B and A is ordered before B. The JMM defines these main rules:

RuleGuarantee
Program orderEach action in a thread happens-before later actions in that same thread
Monitor lockUnlocking a monitor happens-before every later lock of that same monitor
VolatileA write to a volatile field happens-before every later read of that field
Thread startThread.start() happens-before any action in the started thread
Thread joinAll actions in a thread happen-before another thread returns from join() on it
TransitivityIf A happens-before B and B happens-before C, then A happens-before C

Locks and concurrent utilities from java.util.concurrent (such as ExecutorService.submit, Future.get, CountDownLatch and concurrent collections) document similar guarantees built on these rules.

Visibility with volatile

Declaring the flag volatile fixes the example above.

java

A volatile write is visible to subsequent reads, and writes made before the volatile write become visible to a thread that reads the new value. That makes volatile a good fit for flags and safe publication of an immutable object.

volatile does not make compound actions atomic. count++ on a volatile int is still a read, an add and a write, so two threads can lose updates.

Atomicity and mutual exclusion with synchronized

synchronized gives both mutual exclusion and visibility: only one thread holds a given monitor at a time, and unlocking publishes everything written inside the block to the next thread that locks the same monitor.

java

Both methods must use the same lock. If get() were not synchronized, a reader could see a stale value. For simple counters, AtomicInteger or LongAdder is usually faster than a lock.

Data races and what they permit

A data race occurs when two threads access the same variable, at least one access is a write, and there is no happens-before ordering between them. Racy programs are not undefined in Java, but they may see surprising values, reordered operations and, for non-volatile long and double, in principle torn reads. Correctly synchronized programs, those with no data races, behave as if actions run in a single sequential order.

final fields and safe publication

Fields declared final get a special guarantee: if an object is constructed properly and the reference does not escape the constructor, any thread that sees the reference also sees the final fields' correct values, without extra synchronization. This is why immutable classes and records are naturally thread safe.

Common safe-publication techniques are:

  1. Initialize the object in a static initializer.
  2. Store the reference in a volatile field or an AtomicReference.
  3. Store it while holding a lock, and read it under the same lock.
  4. Hand it over through a thread-safe collection or BlockingQueue.

Double-checked locking done right

The classic lazy singleton is only correct with volatile.

java

Without volatile, another thread could see a non-null reference to a partly constructed object. The holder-class idiom or an enum singleton avoids the problem altogether.

Frequently Asked Questions

Is volatile enough for thread safety?

Only for visibility and ordering of a single variable. If an update depends on the previous value, use a lock or an atomic class.

Is the Java Memory Model the same as the JVM heap layout?

No. The heap and stack describe where data is stored at runtime, while the JMM describes visibility and ordering rules between threads.

Does the memory model change with virtual threads?

No. Virtual threads follow the same rules; locks, volatile and the concurrent utilities give the same happens-before guarantees.

How can I test for data races?

Use stress tools such as jcstress for low-level cases, and review code for shared mutable state that lacks a clear happens-before edge.

Summary

The Java Memory Model defines visibility and ordering between threads through the happens-before relation. Use volatile for single-variable flags, synchronized or locks for compound actions, final fields and immutable objects for safe sharing, and the java.util.concurrent classes whenever possible instead of reasoning about raw memory behaviour.

Share