To ensure only one instance of the class get created in a multi-threaded environment, we create a singleton class with the below structure.
class Singleton {
private volatile static Singleton _instance;
private Singleton() {
// preventing Singleton object instantiation from outside
}
public static Singleton getInstanceDC() {
if (_instance == null) {
synchronized (Singleton.class) {
if (_instance == null) {
_instance = new Singleton();
}
}
}
return _instance;
}
}
But is there a way we can improve the performance of the class by getting ride of the penalty that every thread has to pay while doing a null check
before getting the object from the static function ?