乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      單例模式(3)

       頭號碼甲 2020-07-15

      簡介:

      單例模式是一種簡單的設計模式,但是要在程序設計中使用好單例模式,卻需要注意幾個地方。

      單例模式意味著在整個系統(tǒng)中,單例類只能有一個實例對象,且需要自行完成實例化,并始終對外提供同一個實例對象。

      單例模式實現(xiàn)方式:

      餓漢模式:

      public class Singleton {
          //餓漢模式是最簡單的實現(xiàn)方式,在類加載時就創(chuàng)建單例類對象
          private static final Singleton instance = new Singleton();
      
          //私有化構造方法,防止外界創(chuàng)建對象
          private Singleton(){}
      
          public static Singleton newInstance(){
              //返回唯一的實例對象
              return instance;
          }
      }

      懶漢模式(單線程版):

      public class Singleton {
      
          private static Singleton instance = null;
      
          //私有化構造方法,防止外界創(chuàng)建對象
          private Singleton(){}
      
          public static Singleton newInstance(){
              // 在需要的時候才去創(chuàng)建的單例對象,如采羊例對象已經創(chuàng)建,再次調用 ηewinstance()方法時
              // 將不會重新創(chuàng)建新的單例對象,而是直接返回之前創(chuàng)建的單例對象
              if (null == instance){
                  instance = new Singleton();
              }
              //返回唯一的實例對象
              return instance;
          }
      }

      上述懶漢模式有延遲加載的意思,但是沒有考慮到多線程的情況,在多線程的場景中,出現(xiàn)多個線程同時調用newInstance方法創(chuàng)建單例對象,從而導致系統(tǒng)中出現(xiàn)多個單例類的實例,顯然是不符合要求的。

      懶漢模式(多線程加鎖版):

      public class Singleton {
      
          private static Singleton instance = null;
      
          //私有化構造方法,防止外界創(chuàng)建對象
          private Singleton(){}
      
          public static synchronized Singleton newInstance(){
              // 在需要的時候才去創(chuàng)建的單例對象,如采羊例對象已經創(chuàng)建,再次調用 ηewinstance()方法時
              // 將不會重新創(chuàng)建新的單例對象,而是直接返回之前創(chuàng)建的單例對象
              if (null == instance){
                  instance = new Singleton();
              }
              //返回唯一的實例對象
              return instance;
          }
      }

      上述加鎖設計雖然解決了多線程安全的問題,但是單例在系統(tǒng)中只有一個實例,每次取實例都得進行加鎖解鎖操作,這會成為系統(tǒng)的性能瓶頸。

      懶漢模式(雙重檢測鎖方式):錯誤實現(xiàn)

      public class Singleton {
      
          private static Singleton instance = null;
      
          //私有化構造方法,防止外界創(chuàng)建對象
          private Singleton() {
          }
      
          public static Singleton newInstance() {
              if (null == instance) {//第一次檢測
                  synchronized (Singleton.class) {//加鎖
                      if (null == instance) {//第二次檢測
                          instance = new Singleton();
                      }
                  }
              }
              return instance;
          }
      }

      由于指令重排優(yōu)化,可能會導致初始化單例對象和將該對象地址賦值給 instance 字段的順 序與上面 Java 代碼中書 寫 的順序不同 。 例如,線程 A 在創(chuàng)建單例對象時,在構造方法被調用 之前,就為該對象分配了內存空間并將對象的宇段設置為默認值。此時線程 A 就可以將分配的內 存地址賦值給 instance 宇段了,然而該對象可能還沒有初始化。線程 B 來調用 newInstance()方法,得 到的就 是未初始 化 完全 的單例對象,這就 會導致系統(tǒng)出 現(xiàn)異常行為 。

      為了解決該問題,我們可以使用 volatile關鍵字修飾 instance字段。 volatile關鍵字的一個語義就是禁止指令的重排序優(yōu)化,從而保證 instance 字段被初始化時,單例對象己經被完全初始化 。

      懶漢模式(雙重檢測鎖方式 + volatile):

      public class Singleton {
      
          private static volatile Singleton instance = null;
      
          //私有化構造方法,防止外界創(chuàng)建對象
          private Singleton() {
          }
      
          public static Singleton newInstance() {
              if (null == instance) {//第一次檢測
                  synchronized (Singleton.class) {//加鎖
                      if (null == instance) {//第二次檢測
                          instance = new Singleton();
                      }
                  }
              }
              return instance;
          }
      }

      靜態(tài)內部類方式(推薦):

      public class Singleton {
      
          private static class SingletonHolder {
              private static final Singleton instance = new Singleton();
          }
      
          //私有化構造方法,防止外界創(chuàng)建對象
          private Singleton() {
          }
      
          public static Singleton newInstance() {
              return SingletonHolder.instance;
          }
      }

      熟悉 Java 類加載機制 的讀者知道,當?shù)?nbsp;一次訪 問類中的靜態(tài)字段時,會觸發(fā)類加載,并且同一個類只加載一次。靜態(tài)內部類也是如此,類加載過程由類加載器負責加鎖,從而保證線程安全。

       

        本站是提供個人知識管理的網絡存儲空間,所有內容均由用戶發(fā)布,不代表本站觀點。請注意甄別內容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權內容,請點擊一鍵舉報。
        轉藏 分享 獻花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多