Posts

Showing posts from November, 2017

How to make Singleton object- Thread access two singleton object

One point which is worth remembering is that, when we talk about thread-safe Singleton, we are talking about thread-safety during instance creation of Singleton class and not when we call any method of Singleton class. If your Singleton class maintain any state and contains method to modify that state, you need to write code to avoid and thread-safety and synchronization issues. Example 1 Following implementation shows a Singleton design pattern. Singleton class employs a technique known as lazy instantiation to create the singleton; as a result, the singleton instance is not created until the getInstance() method is called for the first time. public class Singleton {    private static Singleton _instance = null;         private Singleton() {}         public static Singleton getInstance() {        if (_instance == null) {            _instance = new Singleton();        }        return _instance;    } } But we have a slight problem with this. Thi