// 单例模式:饿汉式
class Single {
private static final Single s = new Single();
public static synchronized Single getInstance() {
return s;
}
}
// 单例模式:懒汉式
class Single {
private static Single s = null;
public static Single getInstance() {
if (s == null) {
synchronized (Single.class) { //静态方法,锁是Class对象
if (s == null) {
s = new Single();
}
}
}
return s;
}
}
// 单例模式:内部类实现
class Single {
private static class Inner {
private final static Single s = new Single();
}
public static synchronized Single getInstance() {
return Inner.s;
}
}