线程安全

1.双检锁:https://www.cnblogs.com/xz816111/p/8470048.html
优点:安全且在多线程情况下能保持高性能

//正确的双重检查锁
public class Singleton {
    private volatile static Singleton singleton;

    private Singleton() {
    }

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

 

2.乐观锁&悲观锁

posted @ 2019-12-02 20:03  宇枫  阅读(117)  评论(0编辑  收藏  举报