Home Blog Java Notes

Making Singleton thread safe

Published:


Thread Safety in Java Singleton Classes

In general we follow below steps to create a singleton class: 1) Override the private constructor to avoid any new object creation with new operator. 2) Declare a private static instance of the same class 3) Provide a public static method that will return the singleton class instance variable. If the variable is not initialized then initialize it or else simply return the instance variable. package com.journaldev.designpatterns; public class ASingleton { private static ASingleton instance = null; private ASingleton() { } public static ASingleton getInstance() { if (instance == null) { instance = new ASingleton(); } return instance; } } In the above code, getInstance() method is not thread safe. Multiple threads can access it at the same time and for the first few threads when the instance variable is not initialized, multiple threads can enters the if loop and create multiple instances and break our singleton implementation. There are three ways through which we can achieve thread safety. Create the instance variable at the time of class loading. Pros: Thread safety without synchronization Easy to implement Cons: Early creation of resource that might not be used in the application. The client application can’t pass any argument, so we can’t reuse it. For example, having a generic singleton class for database connection where client application supplies database server properties. Synchronize the getInstance() method Pros: Thread safety is guaranteed. Client application can pass parameters Lazy initialization achieved Cons: Slow performance because of locking overhead. Unnecessary synchronization that is not required once the instance variable is initialized. Use synchronized block inside the if loop and volatile variable Pros: Thread safety is guaranteed Client application can pass arguments Lazy initialization achieved Synchronization overhead is minimal and applicable only for first few threads when the variable is null. Cons: Extra if condition Looking at all the three ways to achieve thread safety, I think third one is the best option and in that case the modified class will look like: package com.journaldev.designpatterns; public class ASingleton { private static volatile ASingleton instance; private static Object mutex = new Object(); private ASingleton() { } public static ASingleton getInstance() { ASingleton result = instance; if (result == null) { synchronized (mutex) { result = instance; if (result == null) instance = result = new ASingleton(); } } return result; } } Local variable result seems unnecessary. But it’s there to improve performance of our code. In cases where instance is already initialized (most of the time), the volatile field is only accessed once (due to “return result;” instead of “return instance;”). This can improve the method’s overall performance by as much as 25 percent. If you think there are better ways to achieve this or the thread safety is compromised in the above implementation, please comment and share with all of us.
Comment