推荐用
public class HungrySingleton implements Serializable,Cloneable{
private final static HungrySingleton hungrySingleton;
static{
hungrySingleton = new HungrySingleton();
}
private HungrySingleton(){
}
public static HungrySingleton getInstance(){
return hungrySingleton;
}
}
优点:写法简单,类加载时完成初始化,避免了线程的不同步问题
缺点:没有延迟加载的效果,一加载就初始化,可能这个类至始至终都没有用到,造成了资源的浪费