Double Locking Or Double Check in Singleton
Now these days it is a frequently asked question that how you will create double locking singleton class.
It is nothing new as earlier we were using Synchronize key-work before method, the same key world we will use while creating instance of the singleton class.
See the below written code snap.
private SingletonInstance instance=null;
public SingletonInstance getInstance() {
if (instance == null) {
synchronized (SingletonInstance.class) {
if (instance == null) {
instance = new SingletonInstance();
}
}
}
return instance;
}
This comment has been removed by the author.
ReplyDelete