PrepZone Logo
PrepZone
Back to Java

Java · Hard

Why is volatile necessary in double-checked locking for Singleton?

JavaCore JavaConcurrency

Answer preview

Without volatile, the JVM/CPU can reorder instructions. When creating an object, the JVM may write the reference before the object is fully initialized.…