设计模式之单例模式
2016-04-08 02:12
302 查看
/** * 懒汉式,线程安全单例模式,双重校验锁DCL */ public class Lazy { //禁止new private Lazy() { } //声明静态变量,只执行一次 private volatile static Lazy lazy = null; //公开方法,返回实例对象 public static Lazy getInstance() { if (lazy == null) { synchronized (Lazy.class) { if (lazy == null) lazy = new Lazy(); } } return lazy; } }
/** * 恶汉式单例模式 */ public class Singleton { //禁止new private Singleton() { } //实例化,静态对象,只执行一次 private static Singleton instance = new Singleton(); //公开方法,返回实例对象 public static Singleton getInstance() { return instance; } }
相关文章推荐
- IOS开发--支付宝支付
- 先装.net后装iis的问题
- 通过call_usermodehelper()在内核态执行用户程序【转】
- HDU 1754 I Hate It(线段树区间求最值)
- IDA*
- Java Static关键字
- Coursera Machine Learning Note - Week 3
- Coursera Machine Learning Note - Week 2
- nanakon
- S2-029漏洞GET poc?? 悬赏 5 WB 20160320
- 手机淘宝列表页面 的js调用展示
- 创建Tab页面,建立可切换分页Activity
- #5值传递和址传递,static,instanceof,递归#
- TextView使用大全
- hdu1695(欧拉函数,容斥原理,vector容器)
- unity 运行 iOS模拟器报错 dyld: Symbol not found: __BeginEventWithLabel
- JavaScript学习基础-标签放置位置
- PHP命名空间namespace使用小结
- 史上最全Linux提权后获取敏感信息方法 20140121
- LeetCode 125 Valid Palindrome