public class 单例饿汉 {
private static 单例饿汉 u = new 单例饿汉();
private 单例饿汉() {
}
public static 单例饿汉 createInstance() {
return u;
}
}
public class 单例饱汉 {
static 单例饱汉 u = null;
private 单例饱汉() {
}
public synchronized static 单例饱汉 createInstance() {
if (u == null) {
u = new 单例饱汉();
}
return u;
}
}
原文:https://www.cnblogs.com/alichengxuyuan/p/12578018.html