单例模式双重校验锁

先上代码

public class SingletonTest {
    // 使用volatile保证可见性
    private static volatile SingletonTest singletonTest = null;

    private SingletonTest() {

    }

    public static SingletonTest getInstance() {
        //第一重校验
        if (singletonTest == null) {
            // 加锁
            synchronized (SingletonTest.class) {
                // 第二重校验
                if (singletonTest == null) {
                    singletonTest = new SingletonTest();
                }
            }
        }
        return singletonTest;
    }
}

这里面有2个关键点

1.使用volatile保证可见性

volatile功能:当一个共享变量被volatile修饰时,它会保证修改的值会立即被更新到主存,当有其他线程需要读取时,它会去内存中读取新值

2.使用双重校验

这里为一直无法理解,为什么要加双重校验锁,我原本以为理论上,只要在synchronized里面加一层校验锁,也不会产生单例多线程问题.后面我才理解,如果加了第一层校验,则除例首次需要加锁外,后续都不会进行加锁操作,因为已经获取到了实例.如果只加一层校验锁,则每次获取都要加锁,无疑会加大了许多开销.

原文地址:https://www.cnblogs.com/wangzxblog/p/15302745.html