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

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

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

    單實例模式學(xué)習(xí)筆記


    1. 經(jīng)典的單實例模式例子(非線程安全):
    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
    }
    本例是最經(jīng)典的單實例模式例子,但是在多線程的情況下就會產(chǎn)生多個實例!

    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,會讓該方法是線程安全的,但是會引起每個線程在調(diào)用該方法時的等待,如果getInstance的性能對應(yīng)用程序不是很關(guān)鍵(記住,同步方法可能會使getInstance方法得運行效率降低100倍),本方法是最好得方法!
    3. 提前實例化,不適用延遲實例化(使用于創(chuàng)建和運行時負擔(dān)不太繁重或者應(yīng)用程序總是創(chuàng)建并使用單件實例),它是線程安全得:
    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在加載這個類時候馬上創(chuàng)建此唯一實例,JVM保證在任何線程訪問它之前,一定先創(chuàng)建它!
    4. 在java1.5及以后的版本,增加了volatile關(guān)鍵字,可以采用雙重檢查加鎖!
    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關(guān)鍵字確保:當(dāng)unigueInstance變量在被初始化成實例時,多個線程能夠正確的處理它!(對于關(guān)注性能的程序,這種做法可以大大減少時耗)



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


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


    網(wǎng)站導(dǎo)航:
    博客園   IT新聞   Chat2DB   C++博客   博問  
     
    <2008年5月>
    27282930123
    45678910
    11121314151617
    18192021222324
    25262728293031
    1234567

    導(dǎo)航

    統(tǒng)計

    常用鏈接

    留言簿(2)

    隨筆分類(31)

    隨筆檔案(30)

    文章分類(32)

    文章檔案(33)

    相冊

    PHP小站-首頁

    搜索

    積分與排名

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 亚洲一区影音先锋色资源| 亚洲第一视频在线观看免费| 亚洲一区二区三区香蕉| 黄色一级免费网站| 免费成人午夜视频| 一级做a爰全过程免费视频毛片 | 在线观看亚洲电影| 深夜国产福利99亚洲视频| 亚洲AV成人无码网站| 国产无遮挡吃胸膜奶免费看| 亚洲成a人无码亚洲成www牛牛| 妞干网在线免费视频| 福利片免费一区二区三区| 亚洲午夜爱爱香蕉片| a毛片免费全部在线播放**| 亚洲国产精品成人久久| 久久久精品2019免费观看| 亚洲国产精品xo在线观看| 好男人视频社区精品免费| 亚洲AV无码AV日韩AV网站| 亚洲精品乱码久久久久久不卡| 成全视频在线观看免费| 亚洲国产视频网站| 国产精品酒店视频免费看| 亚州**色毛片免费观看| 亚洲AV乱码一区二区三区林ゆな| 24小时在线免费视频| 亚洲精品无码av片| 相泽亚洲一区中文字幕| 99久久久国产精品免费蜜臀| 国产亚洲精品bv在线观看| 亚洲成?Ⅴ人在线观看无码| 久久免费福利视频| 亚洲午夜无码久久| 亚洲中文字幕不卡无码| 国产成人yy免费视频| 免费人成大片在线观看播放电影| 亚洲啪啪AV无码片| 午夜时刻免费入口| 国产在线精品免费aaa片| 亚洲看片无码在线视频|