Happens-before
Happens-before is the Java Memory Model's rule for visibility and order. If A happens-before B, then B sees A's writes. Without that edge, a read may see a stale value or none of the writes you "already did".
Detailed Explanation
Programmers think in program order. Compilers and CPUs think in whatever order is faster. The JMM defines when a thread is allowed to see another thread's write. The relation is called happens-before. It is not about wall-clock time. It is about a proof that a read cannot miss a write.
Some edges are built in. Each action in a thread happens-before later actions in that same thread (program order). Unlocking a monitor happens-before every later lock of the same monitor. A volatile write happens-before every later volatile read of that field. Starting a thread happens-before the first action in that thread. The last action in a thread happens-before a successful join. A CountDownLatch reaching zero, a Future completing, and many concurrent-collection operations also publish.
The relation is transitive. If you write fields and then unlock, another thread that locks sees those fields. You do not mark every field volatile.
If there is no happens-before edge, the read is a data race (for non-volatile fields). The value can be stale, default, or, for longs and doubles that are not volatile, even a torn mix on some histories. The program is not "a bit racy"; it is undefined for that field.
You reach for this vocabulary when a bug is "the flag never becomes true" or "the object looks half-built". You fix it with a lock, a volatile, a concurrent collection, or safe publication (store to a volatile, final fields after construction, Executor hand-off).
The cost of ignoring happens-before is Heisenbugs that vanish under a debugger (extra fences) and appear on another CPU. The cost of over-synchronizing is speed. Use the smallest edge that publishes the data you need. Do not invent a "memory barrier" API; use the primitives the JMM already defines.
Code Examples
public class Publish {
static int payload;
static volatile boolean ready;
public static void main(String[] args) throws InterruptedException {
Thread writer = new Thread(() -> {
payload = 42; // ordinary write
ready = true; // volatile write publishes payload
});
Thread reader = new Thread(() -> {
while (!ready) {
Thread.onSpinWait();
}
System.out.println(payload); // prints: 42
});
writer.start();
reader.start();
writer.join();
reader.join();
}
}public class JoinPublish {
static int n;
public static void main(String[] args) throws InterruptedException {
Thread t = new Thread(() -> n = 7);
t.start();
t.join(); // join happens-before this read
System.out.println(n); // prints: 7
}
}Common Gotchas
Program order in two threads is not happens-before
you need a lock, volatile, join, or similar edge.
A data race is not "eventually consistent"
the reader may never see the write.
`Thread.sleep` is not a happens-before edge
it just wastes time and still races.
`final` fields are frozen at the end of the constructor
publishing `this` before the constructor finishes loses that guarantee.