Singleton and multithreading in java

Thread safe singleton in java


We all know Singleton pattern ensures a class has only one instance and provides a global point of access to it. But the question is how can we create single instance in case of multi threading ?
Simple answer to this is to make getInstance() method as synchronized method, but by doing this we force every thread to waits its turn before it can enter the method. Here we need to understand two points one is synchronization is expensive and synchronization we done on getInstance() method is only relevant to the first time call of this method, once we get the unique instance we expected, we do not need to synchronize this method.

Now lets see how we can improve this situation in case of multithreading as we often need Singleton in presence of multiple threads.

First thing we can do is we can create eagerly created instance than the lazy instance as shown below :
public class SingletonTest{
       private static SingletonTest uniqueInstance = new SingletonTest();
       private SingletonTest(){
       }
       public static SingletonTest getInstance(){
            return uniqueInstance;
       }  
}

Here in above example we are creating instance of Singleton in a static initialization instead of creating it in getInstance() method i.e. Using lazy initialization. When the class is loaded JVM creates a unique instance of this class and hence it achieves our purpose of not creating multiple instances of Singleton in multithreading environment.

Double checked locking by using volatile keyword

Second thing we can do is to use double checked locking. Lets see what is the double checked locking approach is we first check whether instance is created or not if it is not created then synchronize otherwise not. We achieve this double checked locking by using volatile keyword as shown in example below :

Double checked locking example


public class SingletonTest{
       private volatile static SingletonTest uniqueInstance;
       private SingletonTest(){
       }
       public static SingletonTest getInstance(){
            if( uniqueInstance == null ){
                synchronized ( SingletonTest.class  ){
                       if( uniqueInstance == null ){
                              uniqueInstance = new SingletonTest();
                       } 
                }
            }
            return uniqueInstance;
       }  
}

Volatile keyword make sure multiple treads handle uniqueInstance when it is being initialized. In case of double checked locking before doing synchronization we are checking uniqueInstance as null or not, and in synchronization block also we are checking uniqueInstance is null or not before creating instance.
Share on Google Plus

About Pranav

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment