Sub topic 15
The Java Memory Model and Happens-Before
Earlier chapters showed that one thread may not see another thread's write. This chapter covers the rule Java uses to decide when a write is guaranteed to be seen.
What the Java Memory Model Is
The Java Memory Model (JMM) is the set of rules that says when a value written by one thread must be visible to another thread. Without those rules, the processor and the compiler could reorder your code or keep values in a cache. Both tricks make code faster. They can also hide a write from other threads.
The JMM does not promise that every write is seen at once. It promises visibility only when two actions are linked by a rule called happens-before.
The Happens-Before Rule
Happens-before is a guarantee between two actions. If action A happens-before action B, then everything A wrote is visible to B.
Happens-before does not mean "earlier on the clock." A write can occur first in time and still be invisible to another thread if no happens-before link connects the two.
These are the main rules that create the link:
| Rule | What it guarantees |
|---|---|
| Program order | Within one thread, each line happens-before the next line. |
| Lock | Releasing a lock happens-before any later acquire of the same lock. |
| volatile | A write to a volatile field happens-before any later read of that field. |
| Thread start | Calling `t.start()` happens-before any action inside thread `t`. |
| Thread join | Every action in thread `t` happens-before `t.join()` returns. |
The rules also chain. If A happens-before B, and B happens-before C, then A happens-before C. This chaining is called transitivity.
A Worked Example
public class HandOff {
private static int data = 0;
private static volatile boolean ready = false;
public static void main(String[] args) throws InterruptedException {
Thread reader = new Thread(() -> {
while (!ready) {
// wait until the main thread publishes data
}
System.out.println(data);
});
reader.start();
data = 42;
ready = true;
reader.join();
}
}This program always prints 42. Here is the chain of links:
data = 42happens-beforeready = true(program order).ready = truehappens-before the reader's read ofreadythat seestrue(the volatile rule).- That read happens-before
System.out.println(data)(program order).
By transitivity, the write to data is visible when the reader prints it. The field data is not volatile. It is safe anyway, because the volatile write to ready comes after it and carries it along.
Why This Matters
Every tool in this tutorial works because it creates a happens-before link. synchronized uses the lock rule. volatile uses the volatile rule. Atomic classes and concurrent collections build on these same rules. If you can't trace a happens-before path from a write to a read, assume the read may not see the write.
Exercise
Thread T sets a plain (non-volatile) field result = 7 and then ends. The main thread calls t.join() and then reads result. Is the main thread guaranteed to see 7? Which rule applies?
Show answer
Yes. The thread join rule says every action in T happens-before t.join() returns, so the main thread's read after join() sees result = 7.
Exercise
If ready in HandOff were a plain boolean instead of volatile, what could go wrong?
Show answer
Nothing would link the write to the read. The reader might never see ready become true and loop forever, or it might see ready as true but still print 0 for data.