/**
 * 饿汉式
 */
public class PreLoadSingleton {
    public static PreLoadSingleton instance = new PreLoadSingleton();

    private PreLoadSingleton() {
    }

    public static PreLoadSingleton getInstance() {
        return instance;
    }
}
/**
 * 懒汉式
 */
public class LazyLoadSingleton {
    /**
     * volatile 确保创建对象的顺序性
     */
    private static volatile LazyLoadSingleton instance = null;

    private LazyLoadSingleton() {
    }

    public static LazyLoadSingleton getInstance() {
        if (instance == null) {
            synchronized (instance) {
                if (instance == null) {
                    instance = new LazyLoadSingleton();
                }
            }
        }
        return instance;
    }
}