2026-11-07·作者 Sijie Wang

Java Memory Model and Concurrency

JMM (Java Memory Model): what one thread can see of another thread’s writes — happens-before, volatile, synchronized, final safe publication, and the java.util.concurrent toolkit, rough version.

What it is. JMM is the Java Memory Model. It is the contract that says what one thread can observe of another thread's writes: which writes become visible, and in what order. It does not describe hardware; it describes the guarantees the JVM gives your program, whatever the hardware underneath.

Why it exists. Each thread works with its own registers and CPU caches; main memory is shared. The compiler and the CPU also reorder instructions to run faster. So the order you wrote is not the order other threads see, and a write may sit in one thread's cache and never reach another thread. Without rules, cross-thread communication is guesswork; JMM supplies the rules.

happens-before. The core relation. If action A happens-before action B, then B observes A's effects. The rules that create happens-before include: program order inside one thread; unlocking a monitor happens-before locking the same monitor later; a volatile write happens-before a later volatile read of the same variable; starting a thread happens-before its first action; the last action of a thread happens-before joining it. It is a partial order: actions with no happens-before between them are unordered, and that is a data race.

The volatile write and read are the bridge: the write of x happens-before the volatile write, which happens-before the volatile read, which happens-before the read of x. Without the volatile variable, thread B may never see x == 1.

volatile. A volatile write to a variable happens-before every later volatile read of it. Two guarantees: visibility (the new value is seen by all threads) and a ban on some reordering (nothing before the write moves after it, nothing after the read moves before it). What it does not give: atomicity. count++ on a volatile int is still read-modify-write in three steps, and two threads can interleave between them. For counters use AtomicInteger.

synchronized. Two guarantees in one: mutual exclusion (only one thread inside the block at a time) and visibility (unlocking a monitor happens-before a later lock of the same monitor, so the next thread inside sees everything the previous holder did before leaving). The lock object is the channel; different locks give no ordering between each other.

final and safe publication. A final field, written in the constructor, is visible to every thread that sees the object — provided the reference itself is published without a data race. The values are frozen at the end of construction. This is why immutable objects are the easiest safe way to share data between threads: build it once, publish the reference safely, never mutate.

java.util.concurrent, named only. The toolkit, by name: CAS atomics (AtomicInteger, AtomicReference — lock-free updates by compare-and-swap); ConcurrentHashMap (fine-grained locking, no global lock on reads); thread pools (ExecutorService — a work queue plus worker threads, so threads are reused instead of created per task); Lock (explicit locking with tryLock, fairness, and Conditions). Each is a tool; this node only names them.

Scope. This is the rough version: what each piece is and roughly how it works, not the deep version. Memory barriers, the full JMM cookbook, and proofs such as double-checked locking are not covered here.

關於這一條

sijie 的一條 wiki 條目。本站的 AI 以同一套語料庫為依據、以 sijie 的口吻作答,並附有指向此類條目的引用——回答會花 sijie 的錢,所以它在存取碼後等待: 輸入存取碼 →