/** * 懒汉式,线程安全单例模式,双重校验锁DCL */
public class Lazy {
    //禁止new
    private Lazy() {
    }
    //声明静态变量,只执行一次
    private volatile static Lazy lazy = null;
    //公开方法,返回实例对象
    public static Lazy getInstance() {
        if (lazy == null) {
            synchronized (Lazy.class) {
                if (lazy == null)
                    lazy = new Lazy();
            }
        }
        return lazy;
    }
}
/** * 恶汉式单例模式 */
public class Singleton {
    //禁止new
    private Singleton() {
    }
    //实例化,静态对象,只执行一次
    private static Singleton instance = new Singleton();
    //公开方法,返回实例对象
    public static Singleton getInstance() {
        return instance;
    }
}