Java单例模式的各种写法和最优写法推荐

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/chenbaige/article/details/60973710

话不多说,直接上代码:
1.懒汉模式
只适合单线程环境,在需要的时候才去创建对象实例(时间换空间)。
优点:在不要对象实例的时候,节省了内存空间。
缺点:在使用的时候会先判断是否为空,为空的话,才去创建对象。所以获取对象实例相对较慢。

 class Singleton{

    private static Singleton Instance = null;

     public Singleton() {
     }

     public static Singleton getInstance(){
       if(Instance==null){
           Instance = new Singleton();
       }
        return Instance;
    }
 }

评价:由于只生成一个实例,为防止别人创建该类实例,构造方法为私有属性,只有在实例对象为空时才去创建(为避免重复创建)。

2.饿汉模式
在类编译的时候就创建对象实例,以后使用的时候直接使用(空间换时间)。
优点:获取对象的时候速度快,节省了创建对象的时间。
缺点:不使用也会占用内存空间。

 class Singleton{

    private static Singleton Instance = new Singleton();

     public Singleton() {
     }

     public static Singleton getInstance(){
        return Instance;
    }
 }

评价:开始的时候就创建一个final类型的静态实例对象,以后不会再改变。

3.双重判断

class Singleton{

   private static Singleton Instance = null;

    public Singleton() {
    }

    public static Singleton getInstance(){
      if(Instance==null){
          synchronized (Singleton.class) {
              if (Instance == null) {
                  Instance = new Singleton();
              }
          }
      }
       return Instance;
   }
}

评价:经过两次判定,第一次检测到实例为空时,增加同步,同步后再次检测到实例为空时,才创建对象实例。有效防止了在多线程环境下创建多个实例的问题。

4.volatile 关键字的使用
private static volatile Singleton singleton;
volatile 关键字的使用可以解决一定程度上的多线程访问时出现的同步问题,保证一定程度的线程安全。如果整形的变量由volatile修饰,每次都直接从内存去获取对象(不使用缓存中的),所以保证了每次获取到的都是最新的,线程间透明。

5.结合volatile的单例模式

 class Singleton{
     private static volatile Singleton Instance=null;
     public Singleton() {
     }

     public static Singleton getInstance(){
       if(Instance==null){
           synchronized (Singleton.class) {
               if (Instance == null) {
                   Instance = new Singleton();
               }
           }
       }
        return Instance;
    }
 }

评价:在volatile 和synchronized 两端线程安全的保护下,保证了多线程访问情况下出现多个实例的问题。volatile 保证在实例对象创建后快速通知其他线程
该对象的实例已经存在,不需要重复创建了。主要突出了可见性。

猜你喜欢

转载自blog.csdn.net/chenbaige/article/details/60973710