Java-单例模式 懒汉(线程安全,调用效率不高,但是能延时加载)饿汉(线程安全,调用效率高,但是不能延时加载)双重锁(由于JVM底层模型原因,偶尔会出问题,不建议使用)静态内部类(线程安全,调用效率高,可以延时加载) 懒汉(线程安全,调用效率不高,但是能延时加载) class Test { private static Test test; private Test() { } public static synchronized Test getInstance() { if (test == null) { test = new Test(); } return test; } } 饿汉(线程安全,调用效率高,但是不能延时加载) class Test2 { private static Test2 test = new Test2(); private Test2() { } public Test2 getInstance() { return test; } } 双重锁(由于JVM底层模型原因,偶尔会出问题,不建议使用) class Test3 { private static Test3 test; private Test3() { } private Test3 getInstance() { if (test == null) { synchronized (Test3.class) { if (test == null) { test = new Test3(); } } } return test; } } 静态内部类(线程安全,调用效率高,可以延时加载) class Test4{ private static class SingleClass{ private static final Test4 single = new Test4(); } private Test4(){ } public static final Test4 getInstance(){ return SingleClass.single; } }