public class Singleton{
private static Singleton instance;//静态的实例
private Singleton(){}//私有的构造函数
public static Singleton getInstance(){
if(instance == null){
synchronized(Singleton.class){//锁住整个对象
if(instance==null){//再次判断是否为空,防止没锁对象时判断完后其他线程实例化
instance = new Singleton();
}
}
}
return instance;
}
}