1.标准的单例模式
public class Singleton {
private volatile static Singleton singleton = null; ///volatile 这个变量不会在多个线程中存在复本,直接从内存读取
private Singleton() {
}
public static Singleton getInstance() {
if (singleton== null) {////增加判断条件
synchronized (Singleton.class) {////增加synchronized判断
if (singleton== null) {////增加判断条件
singleton= new Singleton();
}
}
}
return singleton;
}
}
2.但遇到过特例,调取application实例时,多个方法共同调取对象会卡死,具体情况分析:
public synchronized static ApplicationController getInstance() {
if (instance == null) {
instance = new ApplicationController();
}
return instance;
}