单例模式的饿汉式和懒汉式。
1>饿汉式
public class Single{
private static Single s = new Single();
private Single(){};
public static Single getInstance(){
return s;
}
}
2>懒汉式
public class Single{
private static Single = null;
private Single(){};
/*
public static synchornized Single getInstance(){
if(s==null)
s= new Single();
return s;
}
*/
public static Single getInstance(){
if(s==null){
synchornized(Single.class){
if(s==null)
s = new Single();
}
}
return s;
}
}
|
|