I came across the below code as part of creating a singleton using DoubleLocking
public static ThreadSafeSingleton getInstanceUsingDoubleLocking(){
while(instance == null){
synchronized (ThreadSafeSingleton.class) {
while(instance == null){
instance = new ThreadSafeSingleton();
}
}
}
return instance;
}
What is the significance of using while loop
instead of the usual if block
? Is it due to some JMM issues
which might affect the singleton creation in Multi Threaded Environment?