The Java memory model question 14 of 180 mid
Which happens-before edges do you get for free?
- Correct:
Thread.start()andjoin(), monitor unlock to lock, volatile write to read, and final fields - Only
synchronized - Any method call on a shared object
- Anything inside a
tryblock
The list worth memorising, because interviewers ask for it directly:
- Program order inside a single thread.
- Monitor: unlocking a monitor happens-before every later lock of the same monitor.
- Volatile: a write happens-before every later read of that field.
- Thread start: everything before
t.start()happens-before everything int. - Thread join: everything in
thappens-beforet.join()returning. - Final fields: correctly constructed final fields are visible without synchronisation.
- Transitivity: if A happens-before B and B happens-before C, then A happens-before C.
Everything in java.util.concurrent builds on these and documents its own: putting into a
BlockingQueue happens-before taking that element out, submitting a task happens-before it running,
and a CountDownLatch.countDown happens-before a returning await.