代码改变世界

随笔档案-2013年10月

线程安全且高效的单例

2013-10-29 16:15 by Polarisary, 233 阅读, 收藏,
摘要: 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();... 阅读全文