<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    單實例模式學習筆記


    1. 經典的單實例模式例子(非線程安全):
    public class Singleton {
        private static Singleton uniqueInstance;
     
        // other useful instance variables here
     
        private Singleton() {}
     
        public static Singleton getInstance() {
            if (uniqueInstance == null) {
            uniqueInstance = new Singleton();
         }
             return uniqueInstance;
        }
     
        // other useful methods here
    }
    本例是最經典的單實例模式例子,但是在多線程的情況下就會產生多個實例!

    2. 線程安全的例子:
    public class Singleton {
     private static Singleton uniqueInstance;
     
     // other useful instance variables here
     
     private Singleton() {}
     
     public static synchronized Singleton getInstance() {
      if (uniqueInstance == null) {
       uniqueInstance = new Singleton();
      }
      return uniqueInstance;
     }
     
     // other useful methods here
    }
    增加synchronized,會讓該方法是線程安全的,但是會引起每個線程在調用該方法時的等待,如果getInstance的性能對應用程序不是很關鍵(記住,同步方法可能會使getInstance方法得運行效率降低100倍),本方法是最好得方法!
    3. 提前實例化,不適用延遲實例化(使用于創建和運行時負擔不太繁重或者應用程序總是創建并使用單件實例),它是線程安全得:
    public class Singleton {
        private static Singleton uniqueInstance = new Singleton();

        // other useful instance variables here
     
        private Singleton() {}
     
        public static Singleton getInstance() {
                return uniqueInstance;
        }
     
        // other useful methods here
    }
    采用這種方法,我們依賴JVM在加載這個類時候馬上創建此唯一實例,JVM保證在任何線程訪問它之前,一定先創建它!
    4. 在java1.5及以后的版本,增加了volatile關鍵字,可以采用雙重檢查加鎖!
    public class Singleton {
     private volatile static Singleton uniqueInstance;
     
     private Singleton() {}
     
     public static Singleton getInstance() {
      if (uniqueInstance == null) {
       synchronized (Singleton.class) {
        if (uniqueInstance == null) {
         uniqueInstance = new Singleton();
        }
       }
      }
      return uniqueInstance;
     }
    }
    volatile關鍵字確保:當unigueInstance變量在被初始化成實例時,多個線程能夠正確的處理它!(對于關注性能的程序,這種做法可以大大減少時耗)



    posted on 2008-05-04 08:38 扭轉乾坤 閱讀(672) 評論(0)  編輯  收藏 所屬分類: 設計模式


    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    <2008年5月>
    27282930123
    45678910
    11121314151617
    18192021222324
    25262728293031
    1234567

    導航

    統計

    常用鏈接

    留言簿(2)

    隨筆分類(31)

    隨筆檔案(30)

    文章分類(32)

    文章檔案(33)

    相冊

    PHP小站-首頁

    搜索

    積分與排名

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 免费国产草莓视频在线观看黄| 久久久国产精品亚洲一区| 四虎永久在线观看免费网站网址| 亚洲日韩精品一区二区三区| 亚洲一级片免费看| 亚洲一区二区三区影院| 三年片免费观看大全国语| 亚洲真人无码永久在线| a毛片在线免费观看| 亚洲bt加勒比一区二区| 在线人成精品免费视频| 亚洲区视频在线观看| 成人无码精品1区2区3区免费看| 久久伊人亚洲AV无码网站| 国产成人无码区免费内射一片色欲 | 2021国内精品久久久久精免费| 内射少妇36P亚洲区| a毛片基地免费全部视频| 亚洲色大成网站www永久男同| 在线观看亚洲免费| 国产成人精品免费大全| 久久精品国产亚洲AV麻豆不卡| 最近2019年免费中文字幕高清| 亚洲制服丝袜第一页| 玖玖在线免费视频| 亚洲第一页中文字幕| 全免费a级毛片免费**视频| 日本激情猛烈在线看免费观看| 亚洲乱亚洲乱妇无码麻豆| 嫖丰满老熟妇AAAA片免费看| 精品韩国亚洲av无码不卡区| 国产成人精品日本亚洲专区61| 18禁美女裸体免费网站| 午夜亚洲WWW湿好爽| 亚洲国产精品特色大片观看完整版| 免费A级毛片av无码| 综合一区自拍亚洲综合图区| 久久精品国产亚洲av成人| 成人男女网18免费视频| 日韩av无码免费播放| 日韩亚洲人成在线|