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.
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.
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.
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:
| Rule | Guarantee |
|---|---|
| Program order | Each action in a thread happens-before later actions in that same thread |
| Monitor lock | Unlocking a monitor happens-before every later lock of that same monitor |
| Volatile | A write to a volatile field happens-before every later read of that field |
| Thread start | Thread.start() happens-before any action in the started thread |
| Thread join | All actions in a thread happen-before another thread returns from join() on it |
| Transitivity | If 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.
Declaring the flag volatile fixes the example above.
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.
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.
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.
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.
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:
volatile field or an AtomicReference.BlockingQueue.The classic lazy singleton is only correct with volatile.
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.
Only for visibility and ordering of a single variable. If an update depends on the previous value, use a lock or an atomic class.
No. The heap and stack describe where data is stored at runtime, while the JMM describes visibility and ordering rules between threads.
No. Virtual threads follow the same rules; locks, volatile and the concurrent utilities give the same happens-before guarantees.
Use stress tools such as jcstress for low-level cases, and review code for shared mutable state that lacks a clear happens-before edge.
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.