饿汉式和懒汉式

public class SingleTon {
//饿汉式
// private SingleTon(){}
// private final static SingleTon instance=new SingleTon();
// public static SingleTon getInstance(){
// return instance;
// }

//懒汉式
private SingleTon(){}
private static SingleTon instance=null;//new SingleTon()
public static synchronized SingleTon getInstance(){
if(instance==null){
instance=new SingleTon();
}
return instance;
}
}

你可能感兴趣的:(饿汉,懒汉)