java单例模式小结

  1. 双检索实现的单例,是线程安全的。
public class Singleleton {   
   private Singleleton() {}   
   private static Singleleton instance = null;   
   public static Singleleton getInstance() {   
      if(instance == null){    
         synchronize(Singleleton.class){   
            if(instance == null){   
               instance = new Singleton();   
            }   
         }   
      }   
      return instance;   
   }   
}  

  1. 枚举类型实现的单例,目前比较推荐

你可能感兴趣的:(java单例模式小结)