How do you implement a thread-safe Singleton using double-checked locking?

Answer

The naive Singleton with a null check is not thread-safe — two threads can simultaneously see the instance as null and both create a new object. Double-checked locking solves this with minimal synchronization overhead. In Java: declare the instance field as volatile (critical — prevents CPU instruction reordering that could return a partially-constructed object), then check for null outside the synchronized block (fast path), enter the synchronized block, check again inside (slow path), and create if still null. The volatile keyword ensures that once the instance is written, all threads see the fully initialized object. An even simpler thread-safe alternative in Java is the Initialization-on-demand holder idiom: a static inner class holds the instance and is loaded lazily by the JVM class loader, which guarantees thread safety without any synchronization code.