美文网首页
Java设计模式之单例模式

Java设计模式之单例模式

作者: mundane | 来源:发表于2021-03-09 19:28 被阅读0次

1. 静态常量饿汉式

class Singleton {

    private Singleton() {
    }

    private final static Singleton instance = new Singleton();

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

优缺点:
优点: 比较简单, 在类装载的时候完成实例化, 没有线程安全问题
缺点: 在类装载的时候就完成实例化, 没有懒加载的效果, 如果从未使用过这个实 例, 会造成内存的浪费

2. 静态代码块饿汉式

class Singleton {

    private Singleton() {
    }

    private static Singleton instance;

    static {
        instance = new Singleton();
    }

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

优缺点:
和1一样

3. 线程不安全懒汉式

class Singleton {

    private Singleton() {
    }

    private static Singleton instance;


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

优缺点:
优点: 有懒加载的效果, 用到才实例化
缺点: 如果多线程下, 一个线程进入了if(instance == null)的判断语句块, 还未来得及往下执行, 另一个线程也通过了这个判断语句, 就会产生多个实例
结论: 实际开发不推荐

4. 线程安全懒汉式

class Singleton {

    private Singleton() {
    }

    private static Singleton instance;


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

优缺点:
优点: 解决了线程安全问题
缺点: 效率太低了, 每个线程在想获得类的实例的时候, 执行getInstance()方法都要进行同步, 别的线程都要等待
结论: 实际开发不推荐

5. 同步代码块懒汉式

class Singleton {

    private Singleton() {
    }

    private static Singleton instance;


    // 依然不安全
    public static Singleton getInstance() {
        if (instance == null) {
            synchronized (Singleton.class) {
                instance = new Singleton();
            }
        }
        return instance;
    }
}

优缺点:
和3一样, 并不起到线程同步的作用, 假如一个线程进入了if(instance == null)的判断语句块, 还未来得及往下执行, 另一个线程也通过了这个判断语句, 就会产生多个实例

6. 双重检查

class Singleton {

    private Singleton() {
    }

    private static volatile Singleton instance;


    public static Singleton getInstance() {
        if (instance == null) {
            synchronized (Singleton.class) {
                if (instance == null) {
                    instance = new Singleton();
                }
            }
        }
        return instance;
    }
}

注意点:
instance要用关键字volatile修饰, 原因是为了保证instance这个共享变量被修改了之后都能立刻刷新回主内存
优缺点:
优点:

  1. 进行两次if(instance == null)检查保证了线程安全
  2. 实例化代码只用执行一次, 后面再次访问时, 判断if(instance == null)直接return实例化对象, 提高了效率

结论: 线程安全, 延迟加载, 效率较高, 推荐使用

7. 静态内部类

class Singleton {

    private Singleton() {
    }

    private static class SingletonHolder {
        private static final Singleton INSTANCE = new Singleton();
    }


    public static Singleton getInstance() {
        return SingletonHolder.INSTANCE;
    }
}

优缺点:
优点:

  1. 采用类装载的机制来保证初始化实例时只有一个对象
    静态内部类在Singleton类被状态时并不会立即实例化, 而是需要调用getInstance方法, 才会装载SingletonHolder类
  2. 类的静态属性只会在第一次加载类的时候初始化, jvm帮助我们保证了线程的安全性, 在类进行初始化的时候, 别的线程是无法进入的

结论: 避免了线程不安全, 利用静态内部类特点实现延迟加载, 效率较高, 推荐使用

8. 枚举方式

public class SingletonTest08 {

    public static void main(String[] args) {

        Singleton instance = Singleton.INSTANCE;
        Singleton instance2 = Singleton.INSTANCE;

        System.out.println(instance == instance2);
        System.out.println("instance.hashCode() = " + instance.hashCode());
        System.out.println("instance2.hashCode() = " + instance2.hashCode());
    }


}

enum  Singleton {

    // 属性
    INSTANCE;

    public void method() {
        System.out.println("OK");
    }
}

优缺点:
优点: 使用jdk1.5添加的枚举来实现单例模式, 不仅避免多线程同步问题, 还能防止反序列化重新创建新的对象
结论: 推荐使用(然而我在工作中基本没见过这种方式)

总结

推荐使用:
双重检查、静态内部类、枚举
次推荐:
饿汉式
不推荐:
线程不安全懒汉式等

参考

尚硅谷Java设计模式(图解+框架源码剖析)

相关文章

网友评论

      本文标题:Java设计模式之单例模式

      本文链接:https://www.haomeiwen.com/subject/hmubqltx.html