单例模式
来源:互联网 发布:怎样成为淘宝客挣钱 编辑:程序博客网 时间:2024/06/03 14:33
初学单例模式,写的是这种,最简单的懒汉模式
- public class Singleton {
- private static Singleton instance;
- private Singleton (){}
- public static Singleton getInstance() {
- if (instance == null) {
- instance = new Singleton();
- }
- return instance;
- }
- }
后来面试,觉得一直加一种
- public class Singleton {
- private static Singleton instance;
- private Singleton (){}
- public static synchronized Singleton getInstance() {
- if (instance == null) {
- instance = new Singleton();
- }
- return instance;
- }
- }
其实深入来说只有双重校验才能真正的单例模式
- public class Singleton {
- private volatile static Singleton singleton;
- private Singleton (){}
- public static Singleton getSingleton() {
- if (singleton == null) {
- synchronized (Singleton.class) {
- if (singleton == null) {
- singleton = new Singleton();
- }
- }
- }
- return singleton;
- }
- }
阅读全文
0 0
- 单例、单例模式
- 单例模式-多线程单例模式
- 单件模式(单例模式)
- 设计模式------单例模式
- 设计模式------单例模式
- 设计模式-单例模式
- 设计模式 - 单例模式
- 设计模式---单例模式
- 设计模式---单例模式
- PHP模式-单例模式
- 【设计模式】单例模式
- 设计模式-单例模式
- 设计模式----单例模式
- 设计模式--单例模式
- 设计模式-单例模式
- 单例模式(单子模式)
- 设计模式-单例模式
- [设计模式] 单例模式
- mui 模拟自定义事件
- SpringMVC源码(四)Controller控制器2-AbstractController
- 进度条
- Android第三方开源库:PopupWindow相关
- [LeetCode]69. Sqrt(x)
- 单例模式
- csv.Error: iterator should return strings, not bytes
- QtWidget: 自定义不规则窗体与按钮
- Android NavigationBar(菜单栏,导航栏)问题记录
- Java基础入门(四)选择结构
- HDOJ 1003Max Sum (dp)
- 解决Mysql中只能通过localhost登陆不能通过ip登陆的问题
- fragment下找不到getWindowManager() getLayoutInflater()方法
- ldconfig 创建连接名字