The Java memory model question 13 of 180 mid
What is happens-before, in one sentence, and why does it matter?
- It means one statement executes before another in wall-clock time
- Correct: It is an ordering guarantee: if A happens-before B, the effects of A are visible to B and may not be reordered past it
- It only applies to
synchronizedblocks - It is a JVM flag that enables strict ordering
Happens-before is not about time, it is about visibility and ordering. If write A happens-before read B, then B must observe A, and the compiler, JIT and CPU may not move things across that edge in a way you could detect.
Without such an edge there is no guarantee at all. The read may see the new value, the old value, or values in an order the source code never wrote. Nothing is "probably fine": it is unspecified, and unspecified means it will hold on your laptop and break on a 64-core ARM server.
The practical skill is naming the edge. "This is safe because the volatile write to ready
happens-before the volatile read of ready" is an answer. "It works when I run it" is not.