单例模式
0x00、 Singleton Pattern
0x01、 饿汉方式
public class Singleton{
private static Singleton instance = new Singleton();
private Singleton(){}
public static Singleton getInstance(){
return instace;
}
}0x02、 懒汉方式
0x03、 懒汉 · 尝试解决线程安全问题
3.1、 方式一 · 失败的尝试
3.2、 方式二
0x04、 双重校验锁
0x05 提问
5.2、 synchronized
5.3、 关于原子性与可视性 volatile
5.4、 小结
参考
Last updated