//饿汉式
class Single{
private static final Single s=new Single();
private Single(){}
public static Single getInstance(){return s;}
}
//懒汉式
//实例的延迟加载,但在多线程时会出错,要加锁
class Single{
private static Single s=null;
private Single(){}
private static Single getInstance(){
if(s==null){
synchronized(Single.class){
if(s==null){
s=new Single();
}
}
}
return s;
}
} |
|