单例模式的八种实现

2020-02-11  本文已影响0人  夺命404

单例模式有八种实现方式

饿汉式

1.静态常量饿汉式

public class Singleton{
    private static final Singleton SINGLETON = new Singleton();
    private Singleton() {}
    public static Singleton getInstance(){
        return singleton;
    }
}

优点: 实现简单,在类加载时就加载完成,避免了多线程的问题(ps:多个线程的堆共享,但是每个线程的栈是独立的).

缺点: 在类加载时就完成了赋值,没有达到懒加载的效果,不等调用getInstance方法再进行实例化,如果没有调用getInstance方法就造成空间的浪费.

2.静态代码块饿汉式

/**
 * 静态代码块饿汉式
 */
public class Singleton {
    private static Singleton singleton;

    static {
        singleton = new Singleton();
    }

    public Singleton() {}

    public static Singleton getInstance() {
        return singleton;
    }
}

优缺点: == 静态常量饿汉式

懒汉式

1.线程不安全懒汉式

/**
 * 线程不安全懒汉式
 */
public class Singleton {
    private static Singleton singleton;

    public Singleton() {
    }

    public static Singleton getInstance() {
        if (singleton == null) {
            singleton = new Singleton();
        }
        return singleton;
    }
}

优缺点: 起到了懒加载效果,调用方法才进行实例化变量. 但是线程不安全,只适合单线程下使用(开发中不建议使用)

2.线程安全懒汉式

/**
 * 线程安全懒汉式
 */
public class Singleton {
    private static Singleton singleton;
    public Singleton() {}
    public static synchronized Singleton getInstance() {
        if (singleton == null) {
            singleton = new Singleton();
        }
        return singleton;
    }
}

优点: 起到了懒加载的效果,线程安全

缺点: 调用效率低(开发中不建议使用)

  1. 同步代码块懒汉式
/**
 * 同步代码块懒汉式
 */
public class Singleton {
    private static Singleton singleton;
    public Singleton() {}
    public static Singleton getInstance() {
        if (singleton == null) {
            synchronized (Singleton.class){
            singleton = new Singleton();
            }
        }
        return singleton;
    }
}

优点: 懒加载

缺点: 不适合多线程环境,可能因多个线程同时到达if(singleton == null)而产生多个实例,表面代码看线程安全实际线程不安全

其他

1.双重检查(推荐)

/**
 * 双重检查
 */
public class Singleton {
    private static volatile Singleton singleton;
    private Singleton() {}
    public static Singleton getInstance() {
        if (singleton == null) {
            synchronized (Singleton.class){
                if (singleton == null) {
                    singleton = new Singleton();
                }
            }
        }
        return singleton;
    }
}

优点: 解决了同步代码块方式的线程安全问题

  1. 静态内部类(推荐)
/**
 * 静态内部类
 */
public class Singleton {
    private Singleton() {}
    private static class SingletonInstance{
        private static final Singleton singleton = new Singleton();
    }
    public static Singleton getInstance() {
        return SingletonInstance.singleton;
    }
}

优点: 只有在调用公有方法getInstance()的时候才会去装载静态内部类,实例化外部类对象,实现了懒加载,而且外部类对象的实例化是基于JVM对内部类的装载机制,类装载只装载一次,保证单例,同时类装载是线程同步的所以是线程安全的,推荐使用

缺点: 似乎静态内部类看起来已经是最完美的方法了,其实不是,可能还存在反射攻击或者反序列化攻击

  1. 枚举方式(重点推荐)

优优~~点 : 线程安全,效率高,还可防止反序列化重新创建新的对象

public enum  EnumSingleton {
    INSTANCE;
    public EnumSingleton getInstance(){
        return INSTANCE;
    }
}

单例模式的应用场景:

上一篇 下一篇

猜你喜欢

热点阅读