懒汉式:
public class SingDemo {// 懒汉式
private static SingDemo s = null;
private SingDemo() {
}
public static SingDemo getInstance() {
if (s == null) {
synchronized (SingDemo.class) {
if (s == null) {
s = new SingDemo();
return s;
}
}
}
return s;
}
}
饿汉式:
饿汉式:
class SingDemo2 {// 饿汉式
private static SingDemo2 s = new SingDemo2();
private SingDemo2() {
}
public static SingDemo2 getInstance() {
return s;
}
}