单例模式

1.饿汉单例(Eager)

public class Singleton{

    private static Singleton instance = new Singleton();
    
    private Singleton(){
 
    }
   
    public static Singleton getInstance(){
        return instance;
    }
}


2.懒汉单例(Lazy)


public class Singleton{

    private static Singleton instance=null;
    
    private Singleton(){

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


3.懒汉单例升级版1(One Checking)

public class Singleton{

    private static Singleton instance=null;
    
    private Singleton(){

    }
   
    public static Singleton getInstance(){
        if(instance==null){       //1 
           synchronized(Singleton.class){ // 2 
              instance = new Singleton(); // 3 
           }
        }
        return instance;
    }
}


备注:
假设两个线程A,B。当线程A执行到1时候,判断instance==null,执行2,这时线程B执行1检测到instance==null,执行2.然后线程A得到类锁,执行3,线程B等待类锁,线程A创建完对象后释放类锁,线程B得到类锁执行3,再次创建对象,系统此时已经创建了两个对象.所以这个升级版有逻辑错误.


4.懒汉单例升级版2(Double Checking)

public class Singleton{

    private static Singleton instance=null;
    
    private Singleton(){

    }
   
    public static Singleton getInstance(){
        if(instance==null){       //1 
           synchronized(Singleton.class){ // 2
              if(instance==null){         // 3
                  instance = new Singleton(); // 4 
              }
           }
        }
        return instance;
    }
}


备注:
这个版本比较好的解决了上面懒汉单例升级版1中出现的问题,当线程B执行到2的时候等类锁,线程A执行完3,4后释放类锁,线程B得到类锁执行3,检测到instance!=null,不执行创建新的对象.这样看来double checking很好地解决了问题.

但是这里存在这样一个问题,当线程A执行到4的时候

按照常理理解应该是执行下面顺序的伪代码:
1.mem=allocate();//Allocate memory for Singleton object.
2.constructSingleton(instance);//Invoke constructor for Singleton 3.passing instance.
instance=mem;//Note that instance is now non-null,but has not been initialized.

但是也可能会这样执行这样顺序的伪代码:
1.mem=allocate();//Allocate memory for Singleton object. (1)
2.instance=mem;//Note that instance is now non-null,but has not been initialized. (2)
3.constructSingleton(instance);//Invoke constructor for Singleton passing instance. (3)

这段伪代码不仅是可能的,而且是一些 JIT 编译器上真实发生的。执行的顺序是颠倒的,

这是因为Java平台内存模型允许所谓的"无序写入"(参考 http://blog.csdn.net/hudashi/article/details/6949379),如果是按第二种方式的伪代码执行,那么当线程A执行到4的(2)的时候,假设另外一个线程C执行1,检测到instance!=null,直接返回instance,然而线程A还未执行4的(3),所以线程C这里返回的instance所指向的对象并未初始化,使用的时候会出现问题.


5.懒汉单例升级版3(Double Checking Evolution)

public class Singleton{

    private static Singleton instance=null;
    
    private Singleton(){

    }
   
    public static Singleton getInstance(){
        if(instance==null){
           synchronized(Singleton.class){ 
              Singleton inst=instance;
              if(inst==null){           // 1
                 inst = new Singleton();// 2
              }
              instance=inst; //3
           }
        }
        return instance;
    }
}


备注:
貌似解决了上面的问题,但是 1,2,3处的代码可能会被会被jit compiler优化为
   if(inst==null){
      instance= new Singleton();
   }

优化之后的代码同样存在"无序写入"的问题.


6.懒汉单例升级版4(Inner static class)

public class Singleton {
     private Singleton(){
      
     }

     public static Singleton getInstance(){
         return SingletonInner.instance;
     }

     static class SingletonInner{
         static Singleton instance = new Singleton();
     }
}

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