public class SingleDemo {
public static void main(String[] args) {
}
}
//饿汉式
class Single{
private Single(){}
private static Single s = new Single();
public static Single getInstance(){
return s;
}
}
//懒汉式
class Single1{
private Single1(){}
private static Single1 s1 = null;
public static synchronized Single1 getInstance(){
if(s1==null)
s1 =new Single1();
return s1;
}
} |
|