WalzoneInterview Prep
πŸ“ž Interviewing soon? Practice with a realistic AI mock phone interview β€” it calls you, then scores you. First 15 min FREE β†’

Design Patterns Β· Intermediate Β· question 21 of 100

How does the Double-Checked Locking technique work in implementing a thread-safe Singleton pattern?

πŸ“• Buy this interview preparation book: 100 Design Patterns questions & answers β€” PDF + EPUB for $5

The Singleton pattern is a design pattern that restricts the instantiation of a class to one object, ensuring that there is only one instance of the class in the entire application. This pattern is often used in situations where a single object needs to coordinate actions across a system. The implementation of Singleton pattern should be thread-safe to ensure that only one instance of the Singleton class is created in a multi-threaded environment.

The Double-Checked Locking technique is one of the thread-safe mechanisms to implement Singleton pattern. This technique is used to reduce the overhead of acquiring a lock every time the Singleton instance is accessed by the application. The double-checked locking technique involves the following steps:

1. Create a private constructor to restrict the instantiation of the Singleton class.
2. Create a private static volatile variable to hold the Singleton instance. The keyword "volatile" ensures that multiple threads handle the instance variable correctly when it is being initialized to the Singleton instance.
3. Create a public static method to provide a global access point to the Singleton instance. In this method, we first check if the Singleton instance has already been instantiated. If not, then we synchronize on the Singleton class object and check if the Singleton instance is still null. If the Singleton instance is null, we create a new instance of the Singleton class and assign it to the instance variable. Since we are using double-checked locking, we check the instance variable again inside the synchronized block to ensure that it is still null before creating a new instance. After the Singleton instance is created, we release the lock and return the Singleton instance to the caller.

Here’s an example implementation of the Singleton class using the Double-Checked Locking technique in Java:

public class Singleton {
    private static volatile Singleton instance;

    private Singleton() {
        // Private constructor to restrict instantiation
    }

    public static Singleton getInstance() {
        if (instance == null) {
            synchronized (Singleton.class) {
                if (instance == null) {
                    instance = new Singleton();
                }
            }
        }
        return instance;
    }
}

In the code above, the private constructor ensures that the Singleton class cannot be instantiated outside of the class itself. The instance variable is declared as "volatile" to ensure that multiple threads handle it correctly during initialization. The getInstance() method first checks if the instance variable is null before entering the synchronized block. If the instance variable is null, then the method enters the synchronized block and checks the instance variable again. If the instance variable is still null, the method creates a new instance of the Singleton class and assigns it to the instance variable. Finally, the method releases the lock and returns the Singleton instance to the caller.

The Double-Checked Locking technique is an effective way to implement a thread-safe Singleton pattern while minimizing the overhead of acquiring locks every time the Singleton instance is accessed. However, it is important to note that prior to Java 5, it was possible for the instance variable to be non-null before it was fully initialized, resulting in race conditions. As of Java 5, this issue has been resolved by adding the "volatile" keyword to the instance variable declaration.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Design Patterns interview β€” then scores it.
πŸ“ž Practice Design Patterns β€” free 15 min
πŸ“• Buy this interview preparation book: 100 Design Patterns questions & answers β€” PDF + EPUB for $5

All 100 Design Patterns questions Β· All topics