前言
某个类在程序中只存在唯一一份实例,叫做单例模式。
目录
前言
一、饿汉模式
二、懒汉模式
(一)单线程写法
(二)线程安全写法
(三)线程安全和不安全写法的区别
结语
一、饿汉模式
程序启动,类加载之后,立即创建实例,叫做饿汉模式。
代码:
class Singleton{
    private static Singleton singleton = new Singleton();
    public static Singleton getSingleton() {
        return singleton;
    }
    
    private Singleton(){}
}关于代码的注意事项,如图:

二、懒汉模式
在第一次使用实例再创建对象,否则就不创建实例对象,叫做懒汉模式。
(一)单线程写法
代码:
class SingletonLazy{
    private static SingletonLazy singletonLazy = null;
    public static SingletonLazy getSingletonLazy() {
        if(singletonLazy == null){
            singletonLazy = new SingletonLazy();
        }
        return singletonLazy;
    }
    private SingletonLazy(){}
}关于代码的注意事项:

(二)线程安全写法
分析懒汉模式的线程不安全写法:
当多个线程执行时,有如下执行语句的可能:

故,在这种情况下就有两个SingletonLazy 的实例对象的创建,违背了最初的意图,所以此前的写法是线程不安全的。
解决办法是:加锁。
1)根据代码分析,这几条语句最好处理成原子的:

2)故把锁加在 if 语句的外面:

3)考虑到加锁的开销,每次调用该 getSingletonLazy() ,都需要先加锁,再进行判断,所以可以采取在加锁前就进行判断,加锁后也再进行判断,避免重复加锁:

4)避免指令重排序和内存可见性问题,在类变量前加上 volatile :

完整代码是:
class SingletonLazy{
    volatile private static SingletonLazy singletonLazy = null;
    public static SingletonLazy getSingletonLazy() {
        if(singletonLazy == null) {
            synchronized (SingletonLazy.class) {
                if (singletonLazy == null) {
                    singletonLazy = new SingletonLazy();
                }
            }
        }
        return singletonLazy;
    }
    private SingletonLazy(){}
}(三)线程安全和不安全写法的区别
- 加锁;
- 双重 if ;
- volatile.
结语
这篇博客如果对你有帮助,给博主一个免费的点赞以示鼓励,欢迎各位🔎点赞👍评论收藏⭐,谢谢!!!


















