Java Concurrency Interview

The Java memory model question 13 of 180 mid

What is happens-before, in one sentence, and why does it matter?

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.

memory-model happens-before

Quiz yourself on The Java memory model Suggest a correction