码迷,mamicode.com
首页 > 其他好文 > 详细

单例模式之懒汉式

时间:2016-06-27 19:15:31      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:

//懒汉式:
public
class SingleTonDemo { /* * 私有化构造方法 */ private SingleTonDemo() { } /* * 创建对象 */ public static SingleTonDemo instance = null;//不创建对象,需要的时候才会创建 public static SingleTonDemo getInstance() { if (instance == null) {//提高性能 synchronized (SingleTonDemo.class) {//判断锁是否可用,耗资源 if (instance == null) {//判断是否为空 instance = new SingleTonDemo(); } } } return instance; }

 

单例模式之懒汉式

标签:

原文地址:http://www.cnblogs.com/shen-hua/p/5620927.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!