Quick revision
The short answer
Happens-before is the rule in the Java Memory Model that decides when one thread must see another thread's writes: if action A happens-before action B, then A's effects are visible to B and A is ordered before it. The main rules are program order within a thread, monitor unlock before every later lock of the same monitor, a volatile write before every later read of that variable, Thread.start before the started thread's actions, a thread's actions before another thread returns from join on it, and transitivity, plus extra edges documented by java.util.concurrent classes.
What to remember
In this answer, we saw that happens-before is the relation that tells us when a write in one thread is guaranteed to be visible to another, and that without it we have a data race. We went through the main rules: program order, monitor unlock before later lock, volatile write before later read, Thread.start before the new thread's actions, and a thread's actions before join returns. Transitivity lets these edges combine, so a single volatile write can publish many plain writes, and java.util.concurrent classes document further edges so that handing objects between threads through them is safe.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Happens-before is the rule in the Java Memory Model that decides when one thread must see another thread's writes: if action A happens-before action B, then A's effects are visible to B and A is ordered before it. The main rules are program order within a thread, monitor unlock before every later lock of the same monitor, a volatile write before every later read of that variable, Thread.start before the started thread's actions, a thread's actions before another thread returns from join on it, and transitivity, plus extra edges documented by java.util.concurrent classes.
Why this question is asked
When two threads share data, our intuition says a write made by one thread is simply there for the other to read. On real hardware and with a real JIT compiler, that is not true: writes can be delayed, reordered or kept in registers. The Java Memory Model, the part of the language specification that says what a read is allowed to return, replaces intuition with one precise relation called happens-before. Interviewers ask about it because every correct concurrent Java program relies on these rules. In this answer, we will see why we need such a rule, what happens-before means exactly, go through each of the main rules with code, and see how transitivity lets us chain them.
1Why Java Needs a Rule for Visibility
Inside a single thread, Java promises that our code behaves as if it ran in the order we wrote it. To keep that promise cheaply, the compiler and the CPU are allowed to do anything that a single thread cannot notice: keep a variable in a register, reorder two independent writes, or delay a write in a store buffer before it reaches memory shared with other cores. A second thread, however, can notice. It may see the writes in a different order, or not at all.
So the language needs a rule that says when another thread is guaranteed to see a write. That rule is happens-before. If there is no happens-before relation between a write and a read of the same variable in different threads, and at least one of them is a write, we have a data race, and the read may return a surprising value.
2What Happens-Before Means
Happens-before is a relation between two actions, such as a write to a field and a read of it. If A happens-before B, two things hold: the results of A are visible to B, and A is ordered before B as far as B can tell. It is not about wall-clock time. A write can happen earlier in real time and still not happen-before a read in another thread, in which case the read may not see it.
3Program Order, Locks and Volatile
The first rule is program order: each action in a thread happens-before every later action in the same thread. This is what makes single-threaded code behave as written.
The second rule is about monitors, the locks behind synchronized. An unlock of a monitor happens-before every later lock of that same monitor. The third rule is the volatile equivalent: a write to a volatile field happens-before every later read of that field that sees the value.
Notice that the lock rule only works when both threads use the same monitor. A synchronized method on one object gives no guarantee to code that locks a different object. Likewise, the volatile rule applies to reads of the same variable that observe the write; reading a different volatile field creates no edge.
4Thread Start, Join and Other Built-In Edges
Starting and finishing threads also create edges. A call to Thread.start() happens-before every action in the started thread, so the new thread sees everything its parent did before starting it. All actions in a thread happen-before another thread returns from join() on it, so after join() returns we see everything the finished thread wrote.
| Rule | Edge from | Edge to |
|---|---|---|
| Program order | An action in a thread | Every later action in that thread |
| Monitor lock | Unlock of monitor M | Every later lock of the same M |
| Volatile | Write of volatile v | Every later read of v |
| Thread start | Call to t.start() | Every action in t |
| Thread join | Every action in t | Return from t.join() |
| Library classes | e.g. queue.put(x) | The take() that receives x |
The specification has a few more rules, such as those for interrupts and final fields, but these are the ones we use every day.
5Transitivity: Chaining the Edges
The rules become powerful because happens-before is transitive: if A happens-before B and B happens-before C, then A happens-before C. This is how a single volatile write can publish many ordinary writes.
- Thread 1 writes a = 1, b = 2program order
- Thread 1 writes volatile ready = truerelease point
- Thread 2 reads ready and sees truevolatile rule edge
- Thread 2 reads a and bsees 1 and 2 by transitivity
4 steps in order. Play them, or step through them one at a time.
Writes (1) and (2) are plain, but they happen-before the volatile write (3) by program order. The write (3) happens-before the read (4) by the volatile rule, and (4) happens-before the print by program order. Chaining these gives an edge from (1) and (2) to the print, so Thread 2 must see both values. If we removed volatile from ready, the chain would break in the middle, and Thread 2 could see ready as true while still seeing a or b as 0.
6Edges Documented by java.util.concurrent
We rarely build these chains by hand. The classes in java.util.concurrent document their own happens-before guarantees, which are called memory consistency effects in their Javadoc. Actions in a thread before putting an object into a concurrent collection, such as a BlockingQueue or a ConcurrentHashMap, happen-before actions after that object is accessed or removed in another thread. Actions before submitting a task to an ExecutorService happen-before the task runs, and actions in the task happen-before the result is retrieved through Future.get(). Releasing a lock, counting down a latch or completing a CompletableFuture create similar edges.
So when we hand an object to another thread through a queue, the receiver sees the object fully built, even if its fields are plain. That is the practical lesson: synchronization is about creating edges, and the library creates them for us when we use it correctly.
7Follow-up questions
Does happens-before mean the first action really ran earlier in time?
No. It is a guarantee about visibility and ordering as observed by the threads involved, not about clocks. Two actions with no happens-before edge may run in either order, or appear to different threads in different orders. And the JVM may reorder actions that are ordered by happens-before, as long as no correctly synchronized thread could observe the difference.
What is a data race in terms of happens-before?
Two accesses to the same variable from different threads, at least one a write, that are not ordered by happens-before. A program with no data races is called correctly synchronized, and for such programs the JMM guarantees sequential consistency: every execution looks like some interleaving of the threads in program order. That is the property that lets us reason about synchronized code with simple interleavings.
8Conclusion
In this answer, we saw that happens-before is the relation that tells us when a write in one thread is guaranteed to be visible to another, and that without it we have a data race. We went through the main rules: program order, monitor unlock before later lock, volatile write before later read, Thread.start before the new thread's actions, and a thread's actions before join returns. Transitivity lets these edges combine, so a single volatile write can publish many plain writes, and java.util.concurrent classes document further edges so that handing objects between threads through them is safe.
Open with
Happens-before is the relation the Java Memory Model uses to define visibility between threads. If action A happens-before action B, then A's effects are visible to B and A appears ordered before B.
Then, if they want more
If two threads access the same variable, one of them writes, and there is no happens-before between them, that is a data race and the read can return a stale or surprising value. The main rules are these. Program order: within one thread, each action happens-before the later ones.
Monitors: an unlock happens-before every subsequent lock of the same monitor, so synchronized on the same object makes writes visible. Volatile: a write to a volatile field happens-before every later read of that field. Thread start: everything before t.start happens-before the actions in t. Join: everything t does happens-before another thread returns from t.join.
And the relation is transitive, which is the important part in practice: plain writes followed by a volatile write are visible to a thread that reads the volatile and sees the new value.
On top of that, java.util.concurrent classes document extra edges, for example putting an object into a BlockingQueue or ConcurrentHashMap happens-before another thread takes it out, and submitting a task to an executor happens-before the task runs. So when I reason about concurrent code, I look for the edge that connects each write to each read in another thread.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- JLS chapter 17, Threads and Locks
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "What is the happens-before relation in the Java Memory Model, and list the main rules...": https://scrivio-interview-library.netlify.app/java/java-memory-model/a0430bbd5c/