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.
Previous
How does the Observer pattern relate to event-driven programming?
Next
How do the SOLID principles relate to GoF patterns?
More Design Patterns (Gang of Four) Questions
View all →- Advanced How do the SOLID principles relate to GoF patterns?
- Advanced What is the difference between GoF anti-patterns and the patterns themselves?
- Advanced How are GoF patterns used in modern frameworks like Spring and Django?
- Advanced What criteria should guide pattern selection?
- Advanced How do GoF patterns apply in functional programming?