`
rockyuse
  • 浏览: 192011 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

java单例模式

 
阅读更多
public class Singleton {  
  private static Singleton s=new Singleton(); 
  private Singleton(){};
  /**
   * Class method to access the singleton instance of the class.
   */
  public static Singleton getInstance() {
    return s;
  }
}

 

 

public class Singleton {

	private volatile static Singleton singleton;
	
	private Singleton(){ }
	
	public static Singleton getInstance(){
		// 双重检查加锁
		if(singleton==null){
			synchronized(Singleton.class){
				// 延迟实例化,需要时才创建
				if(singleton==null)
					singleton = new Singleton();
			}
		}
		return singleton;
	}
}

 

 

http://www.iteye.com/topic/897652

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics