package static_demo;
public class SingleObject {
public static void mian(){
}
}
//线程安全 ——赖汉式
class Singleton {
private static Singleton instance;
private Singleton (){}
public static synchronized Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}
//这种写法能够在多线程中很好的工作,而且看起来它也具备很好的lazy loading,但是,遗憾的是,效率很低,99%情况下不需要同步
//饿汉式
class Singleton1 {
private static Singleton1 instance = new Singleton1();
private Singleton1 (){}
public static Singleton1 getInstance() {
return instance;
}
}
|