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.乐观锁&悲观锁