多线程下的单例
2016-05-27 15:22
197 查看
1多线程安全单例模式一(不使用同步锁).
2.多线程安全单例模式一(使用同步锁).
3.多线程安全单例模式一(使用双重同步锁).
1 1 public class Singleton { 2 2 private Singleton() 3 3 {} 4 4 private static Singleton singleton; 5 5 6 6 public static Singleton getInstance() 7 7 { 8 8 if(singleton ==null) 9 9 { 10 10 singleton =new Singleton(); 11 11 } 12 12 return singleton; 13 13 } 14 14 15 15 16 16 }View Code
2.多线程安全单例模式一(使用同步锁).
public class Singleton { private Singleton() {} private static Singleton singleton; //sychronized 同步 public static synchronized Singleton getInstance() { if(singleton ==null) { singleton =new Singleton(); } return singleton; } }
3.多线程安全单例模式一(使用双重同步锁).
public class Singleton { private static Singleton instance; private Singleton (){ } public static Singleton getInstance(){ //对获取实例的方法进行同步 if (instance == null){ synchronized(Singleton.class){ if (instance == null) instance = new Singleton(); } } return instance; } }
相关文章推荐
- Cocoapods的Podfile常见语法总结
- 找水王2
- css3 动画demo
- Java反射学习总结
- Java设计模式——策略模式
- Python list vs set
- jquery ui 的 tabs 祛除蓝色高亮边框
- Java正则常用规则整理说明
- HDOJ/HDU 1161 Eddy's mistakes(大写字母转换成小写字母)
- 欢迎使用CSDN-markdown编辑器
- HDOJ/HDU 1161 Eddy's mistakes(大写字母转换成小写字母)
- 研究博客表的结构
- 8.9.10
- 适配器模式和php实现
- SVG transform坐标变换
- Java transient关键字
- Android学习系列(23)--App缓存管理
- CSDN 账户内的文章全部不见了
- 【EasyUI-datagrid数据展示+MongoDB数据操作】
- StudyFoxCMS-6