多线程下单例模式的正确写法
2016-04-25 21:53
288 查看
package com.peanut.singleton; /** * 多线程下正确的单例模式写法 * Created by peanut on 2016/4/25. */ public class SingletonDemo { private SingletonDemo() { } //synchronized private static SingletonDemo instance; private synchronized static SingletonDemo getInstance() { if (instance == null) instance = new SingletonDemo(); return instance; } //2、volatile+双重检查锁定 private volatile static SingletonDemo instance1; private static SingletonDemo getInstance1() { if (instance1 == null) { synchronized (SingletonDemo.class) { if (instance1 == null) { instance1 = new SingletonDemo(); } } } return instance1; } //3、静态内部类实现 private static class SingletonHolder { private static SingletonDemo instance2 = new SingletonDemo(); } private static SingletonDemo getInstance2() { return SingletonHolder.instance2; } }
相关文章推荐
- 参数修饰符
- 团队作业---软件制作5
- 连接池
- Dubbo学习(十一):结果缓存
- zoj 3946 Highway Project【SPFA多个性质的最优化】
- Netty in action thrid chapter 心得总结
- UVA_12210_A Match Making Problem
- iOS ----新特性 3DTouch 开发教程全解(含源码)
- JS+CSS3人物奔跑动画
- 团队项目:个人工作总结07
- ServletContext在tomcat启动的时候创建
- SQL 日期格式化
- Android5.0的RecycleView技术
- 软件过程模型(生命周期模型)
- 面试题1
- 【定时任务】JDK java.util.Timer定时器的实现原理
- 毛玻璃效果随着tableView滑动清晰
- ACdream 1157 Segments 【CDQ分治】
- 每周总结
- 第一冲刺阶段站立会议09