单例模式

作者: rbty | 来源:发表于2019-07-16 20:09 被阅读0次

单例模式

  • 单例模式可以分为饿汉式和懒汉式
  • 饿汉式会在类装载的时候变完成实例化,如果从未使用过这个实例则会造成内存浪费
  • 而懒汉式则是在需要的时候由使用者自行创建实例,这里的问题是如何在多线程环境下保证单例

单例模式的实现方式

饿汉式(静态常量)

注:静态常量位于虚拟机内存的方法区,是线程共享的。

public class Singleton {

    private final static Singleton INSTANCE = new Singleton();

    private Singleton(){}

    public static Singleton getInstance(){
        return INSTANCE;
    }
}
懒汉式(线程不安全)

实现懒汉式单例,我们首先想到的是构造函数私有化以及获取唯一实例的方法

public class Singleton {

    private static Singleton singleton;

    private Singleton() {}

    public static Singleton getInstance() {
        if (singleton == null) {
            singleton = new Singleton();
        }
        return singleton;
    }
}
懒汉式(方法锁,线程安全)

于是为了保证线程安全,我们在获取实例的方法上加锁,这样的确可以保证线程安全,但是这里的同步块并非最小粒度

public class Singleton {

    private static Singleton singleton;

    private Singleton() {}

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

于是我们理所当然的想到了这种错误的方式,他的问题在于若两个线程同时到达 if (singleton == null) 则会创建两个实例。

public class Singleton {

    private static Singleton singleton;

    private Singleton() {}

    public static Singleton getInstance() {
        if (singleton == null) {
            synchronized (Singleton.class) {
                singleton = new Singleton();
            }
        }
        return singleton;
    }
}
懒汉式(双重检查,仍然线程不安全)

接下来的方案仍然是线程不安全的,但是理解他的问题我们需要引入指令重排序的概念。

  • 指令重排序 简单的来说就是,由于cpu的效率远高于内存,为了提高代码的执行速度,jvm会在不影响最终结果的前提下,对需要执行语句进行顺序调整。
  • 然后我们看代码,在执行instance = new Singleton2() 的时候,jvm大概租了三件事:1分配内存、2创建实例、3将instance指向分配的内存空间。
  • 我们无法保证执行顺序一定是123,如果被重排序为了132,在3执行完毕时,另一个线程进入同步块 判断install是否为空时,因instace已经指向了分配的内存空间,会得到false,返回一个未完成实例化的instance。
public class Singleton2 {

    private Singleton2(){}
    
    private static Singleton2 instance;

    public static synchronized Singleton2 getInstance(){
        if(instance == null) {
            synchronized (Singleton2.class){
                if (instance == null){
                    instance = new Singleton2();
                }
            }
        }
        return instance;
    }
}
懒汉式(双重检查禁止重排序)

对于多线程下的重排序问题java给出的解决方案是volatile关键字,他保证了在写操作完成前,不会对其进行读操作。

public class Singleton2 {

    private Singleton2(){}
    
    private static volatile Singleton2 instance;

    public static synchronized Singleton2 getInstance(){
        if(instance == null) {
            synchronized (Singleton2.class){
                if (instance == null){
                    instance = new Singleton2();
                }
            }
        }
        return instance;
    }
}
静态内部类
public class Singleton2 {

    private Singleton2(){}
    
    public static class SingletonHolder{
        private static final Singleton2 INSTANCE = new Singleton2();
    }
    
    public static Singleton2 getInstance(){
        return SingletonHolder.INSTANCE;
    }
}

相关文章

  • 【设计模式】单例模式

    单例模式 常用单例模式: 懒汉单例模式: 静态内部类单例模式: Android Application 中使用单例模式:

  • Android设计模式总结

    单例模式:饿汉单例模式://饿汉单例模式 懒汉单例模式: Double CheckLock(DCL)实现单例 Bu...

  • 2018-04-08php实战设计模式

    一、单例模式 单例模式是最经典的设计模式之一,到底什么是单例?单例模式适用场景是什么?单例模式如何设计?php中单...

  • 设计模式之单例模式详解

    设计模式之单例模式详解 单例模式写法大全,也许有你不知道的写法 导航 引言 什么是单例? 单例模式作用 单例模式的...

  • Telegram开源项目之单例模式

    NotificationCenter的单例模式 NotificationCenter的单例模式分析 这种单例模式是...

  • 单例模式Java篇

    单例设计模式- 饿汉式 单例设计模式 - 懒汉式 单例设计模式 - 懒汉式 - 多线程并发 单例设计模式 - 懒汉...

  • IOS单例模式的底层原理

    单例介绍 本文源码下载地址 1.什么是单例 说到单例首先要提到单例模式,因为单例模式是单例存在的目的 单例模式是一...

  • 单例

    iOS单例模式iOS之单例模式初探iOS单例详解

  • 单例模式

    单例模式1 单例模式2

  • java的单例模式

    饿汉单例模式 懒汉单例模式

网友评论

    本文标题:单例模式

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