Skip to main content
PreparationZone Logo
PreparationZone
Back to Java

Java · Medium · Free

volatile happens-before — safe publication of objects?

JavaConcurrencyFresher
Model answer

volatile and publication

volatile ensures visibility and ordering (happens-before) for reads/writes. It does not compose atomic read-modify-write—use atomics for counters.

  • Safe publication of immutable object reference after construction.
  • Double-checked locking requires volatile instance field.
  • Not a substitute for synchronizing compound invariants.
  • JMM guarantees readers see latest written value.
java
private volatile boolean shutdown;

void shutdown() { shutdown = true; }

boolean isShutdown() { return shutdown; }
Pair with immutable objects—volatile reference + final fields inside = safe publish.