【转】高并发情况下的单例模式

如果在高并发时候,使用这种单例模式 
publci class Singleton{ 
     private static Singleton instance = null; 
     private Singleton(){} 

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

可能会出现多个指向改类的对象,这是什么情况呢? 

 
从上面图可以看到,在1、2情况都没有创建对象,到了3时候Thread1创建一个对象,而Thread2并不知道,所以在4的情况下面Thread2也创建了对象,所以就出现该类不同对象,如果是使用C++语言实现这种模式,而且没有手工去回收就可能出现内存泄露情况。解决的方法是使用关键字synchronized代码如下: 
publci class Singleton{ 
     private static Singleton instance = null; 
     private Singleton(){} 

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

这样一来不管多少个线程访问都是实现一个对象实例化了。但是如果使用该关键字可能性能方面有所降低,因为每次访问时候都只能一个线程获取到该对象,当出现多个线程访问时候就会出现排队等待的情况,为了解决这种情况只需要在创建时候使用该关键字就可以了 
publci class Singleton{ 
     private static Singleton instance = null; 
     private Singleton(){} 

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

因为第一次使用该对象时候才需要检查该对象是否已经创建了,而第二次检查改对象是否为空是为了避免1、2的情况,因为不管是Thread1或者是Thread2拿到线程锁都不会阻止另外的线程创建对象,因为到了2的情况中,如果Thread1已经拿到线程锁之后,创建对象但是到了Thread2获取到线程锁时候,也创建对象所以也会出现不同对象实例的情况,这种两次检查叫做double click locking模式

原文地址:https://www.cnblogs.com/xingluzhe/p/4723255.html