0
点赞
收藏
分享

微信扫一扫

Redis之INCR命令,通常用于统计网站访问量,文章访问量,分布式锁

DYBOY 2023-12-19 阅读 42

目录

什么是设计模式?

1.饿汉模式

2.懒汉模式

线程安全问题


什么是设计模式?

单例模式能保证某个类在程序中只存在唯⼀⼀份实例, ⽽不会创建出多个实例. 这⼀点在很多场景上都需要. ⽐如 JDBC 中的 DataSource 实例就只需要⼀个. 

单例模式具体的实现⽅式有很多. 最常⻅的是 "饿汉" 和 "懒汉" 两种.

1.饿汉模式

所谓 "饿" 形容 "非常迫切" , 实例实在类加载的时候就创建了, 创建时机非常早, 相当于程序一启动 , 实例就创建了, 就使用 "饿汉" 形容创建实例非常早. 

class Singleton {
    private static Singleton instance = new Singleton();
    
    public static Singleton getInstance() {
        return instance;
    }
    
    private Singleton(){}; //阻止后续代码new出新的实例
}

2.懒汉模式

类加载的时候不创建实例. 第⼀次使⽤的时候才创建实例.

// 懒汉模式实现单例模式
class SingletonLazy {
    // 这个引用指向唯一的实例, 先初始化为null, 而不是立即创建实例
    private static SingletonLazy instance = null;

    public SingletonLazy getInstance() {
        if(instance == null) {
            instance = new SingletonLazy();
        }

        return instance;
    }

    private SingletonLazy() {};
}

如果是首次调用 getInstance , 那么此时 instance 引用为 null, 就会进入 if 条件, 从而把实例创建出来. 如果是后续再次调用 getInstance , 由于instance 已经不再是 null, 此时不会进入 if , 直接返回之前创建好的引用了. 

这样设定, 既可以保证该类的实例是唯一一个, 于此同时, 创建实例的实际就不是程序驱动时了, 而是第一次调用 getInstance 的时候. 

线程安全问题

对于饿汉模式来说, getInstance 方法直接返回 Instance 实例, 这个操作本质就是"读操作", 多个线程读取同一个变量, 是线程安全的.

在懒汉模式中, 如下图所示,

有可能会创建出多个实例, 是线程不安全的.

下面是一个改进的代码: 

class SingletonLazy {
    private static SingletonLazy instance = null;
    private static Object locker = new Object();
    public static SingletonLazy getInstance() {
        synchronized (locker) {
            if(instance == null) {
                instance = new SingletonLazy();
            }
            return instance;
        }
    }
    private SingletonLazy(){ };
}

这个代码虽然解决的线程不安全的问题, 但是每次执行这串代码的时候都要进行加锁解锁的操作, 这样程序的效率就变差了, 如果在加锁前先进行判断是否需要加锁, 就可以提高程序的效率了.

再次改进后的代码:

class SingletonLazy {
    private static SingletonLazy instance = null;
    private static Object locker = new Object();
    public static SingletonLazy getInstance() {
        if(instance == null) {
            //如果 instance 为 null, 就说明是首次调用, 首次调用就要考虑线程安全问题, 就要加锁
            //如果是非 null, 就说明是后续调用, 就不用加锁了
            synchronized (locker) {
                if(instance == null) {
                    instance = new SingletonLazy();
                }
            }
        }
        return instance;
    }
    private SingletonLazy(){ };
}

然而, 这段代码还有进一步改进的空间!

解决上述问题核心方法是使用 volatile 关键字

最终修改后的代码: 

class SingletonLazy {
    public volatile SingletonLazy instance = null;
    Object locker = new Object();
    public SingletonLazy getInstance() {
        if(instance == null) {
            synchronized (locker) {
                if(instance == null) {
                    instance = new SingletonLazy();
                }
            }
        }
        return instance;
    }

    private SingletonLazy(){}
}

 

举报

相关推荐

0 条评论