Under what circumstances, we need to use the lazy singleton mode. It seems that in the process of use, we load a class to use its properties or methods, then we must create its instance at this time, so there is no difference between a hungry man and a lazy man. In what scenario do we need to load It seems that a class is always loaded the first time it is used
My question is : Under what circumstances, a singleton class will be loaded by jvm but not used. It seems that jvm always loads this class when it is used for the first time
class Main{
public static void main(String[] args) {
Singleton instance=Singleton.getInstance();
Singleton1 singleton1=Singleton1.getInstance();
}
}
class Singleton1{
private Singleton1(){}
private volatile static Singleton1 instance=null;
public static Singleton1 getInstance(){
if(instance==null){
synchronized (Singleton1.class){
if(instance==null){
instance=new Singleton1();
}
}
}
return instance;
}
}
class Singleton{
private Singleton(){}
private static Singleton instance=new Singleton();
public static Singleton getInstance(){
return instance;
}
}
There seems to be no difference between the two
Hope to get some lazy singleton application scenarios