Interview Question

Explain happens-before in simple terms.

Happens-before is the visibility and ordering guarantee created by synchronization rules.

💡 Concept ✅ Quick Revision ☕ Java

Answer

Happens-before is a guarantee that effects of one action are visible to another action in a defined order. • Program order creates happens-before within one thread. • Unlocking a monitor happens-before a later lock of the same monitor. • A volatile write happens-before a later read of the same volatile field.

Example

Code
class State {
    int value;
    volatile boolean ready;
    void publish() { value = 42; ready = true; }
}

Quick Revision

Happens-before is the visibility and ordering guarantee created by synchronization rules.