1 饿汉式
package com.designmode.single;
public class HungrySingle {
private static HungrySingle instance=new HungrySingle();
private HungrySingle() {
}
public static HungrySingle getInstance() {
return instance;
}
public void outMsg() {
System.out.println("HungrySingle");
}
}
package com.designmode.single;
/**
* 双重检查
* @author huhu
*
*/
public class DbCheckSingle {
private volatile static DbCheckSingle singleton;
private DbCheckSingle (){}
public static DbCheckSingle getSingleton() {
if (singleton == null) {
synchronized (DbCheckSingle.class) {
if (singleton == null) {
singleton = new DbCheckSingle();
}
}
}
return singleton;
}
}