Singleton和Double Check

來源:互聯網
上載者:User

首先看一段比較經典的代碼: public   static  Singleton getInstance()
{
   if  (instance  ==   null ) //0
  {
     synchronized (Singleton. class ) {  
       if  (instance  ==   null )          
        instance  =   new  Singleton();  
    }
  }
   return  instance;
}

在這段代碼裡,對instance == null 的兩次檢查就是我們所說的Double Check. 而對於為什麼要做兩次,網上的資料不勝枚舉,這裡就不多說了。

現在的問題是,這樣的double check 並不能保證安全執行緒。

關鍵在於Java 變數初始化以及賦值的順序。簡單地說,上面的程式中, instance = new Singleton() 這一句的操作過程是這樣的:

1。 為instance 分配空間

2。 將instance 指向港分配的空間

3。 為instance 賦值

可以看到,如果在完成步驟2 的時候,有一個新線程進入到上面程式中注0 的地方。那麼,將會發生的情況是不可預料的。

道理很簡單。可惜以訛傳訛的情況在網上太多了。

至於解決方案,只有定義執行個體變數的時候就完成初始化和賦值。比如: class  Singleton
... {
  private Vector v;
  private boolean inUse;
  private static Singleton instance = new Singleton();

  private Singleton()
  ...{
    v = new Vector();
    inUse = true;
    //...
  }

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

或者,老老實實用synchronized 方法吧。

REF: http://www.ibm.com/developerworks/library/j-dcl.html

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.