隨筆-95  評論-31  文章-10  trackbacks-0
           1/**
           2 * @author lx
           3 * 線程不安全的單例,試想兩個線程都進入了if(singleton==null)塊里這個時候會初始化兩個對象,這只在第一次調用的時候會產生
           4 */

           5public class Unsafe_singleton {
           6
           7    private static Unsafe_singleton singleton;
           8
           9    private Unsafe_singleton() {
          10    }

          11
          12    public static Unsafe_singleton getInstance() {
          13        if (singleton == null{
          14            singleton = new Unsafe_singleton();
          15        }

          16        return singleton;
          17    }

          18}

          19public class Safe_singleton {
          20     
          21    private static Safe_singleton singleton;
          22
          23    private Safe_singleton() {
          24    }

          25
          26    /**
          27     * 這種寫法,非常消耗性能,因為只是第一次進來的時候可能會產生多個實例的情況,后面多線程都不會再進行實例化,那么調用的時候會很降低性能
          28     * @return
          29     */

          30    public static synchronized Safe_singleton getInstance() {
          31        if (singleton == null{
          32            singleton = new Safe_singleton();
          33        }

          34        return singleton;
          35    }

          36}

          37/**
          38 * @author lx
          39 * 這種寫法如果在創(chuàng)建和運行時的負擔不太繁重,那么這種寫法也可以保證多線程安全,每個線程進來都會new 創(chuàng)建此實例。
          40 * 這種單例相對于每個線程來說確實是唯一的
          41 */

          42public class MoreSafe_singleton {
          43        
          44    private static MoreSafe_singleton singleton = new MoreSafe_singleton();
          45    
          46    public static MoreSafe_singleton getInstance(){
          47        return singleton;
          48    }

          49}

          50/**
          51 * @author lx
          52 * volatile 這個只在JDK1.5以上出現(xiàn) 它確保singleton變量被初始化時,多個線程正確地處理singleton變量
          53 * 這種寫法如果性能是關注的重點,那么可以大大減少getInstance()的時間消耗
          54 */

          55public class VerySafe_singleton {
          56     
          57    private volatile static VerySafe_singleton singleton;
          58    private VerySafe_singleton(){}
          59    
          60    public static VerySafe_singleton getInstance(){    
          61        if(singleton==null){
          62            synchronized(VerySafe_singleton.class){    
          63                if(singleton==null)//里面為何在加上判斷是否為空?
          64                    //因為試想如果有三個線程其中一個線程剛剛走出synchronized塊,這個時候又進來一個線程,
          65                    //如果不判斷是否為空,這個又會實例一個變量那么就不是單例了
          66                    singleton=new VerySafe_singleton();
          67                }

          68            }

          69        }

          70        return singleton;
          71    }

          72}
          posted on 2010-09-02 15:44 朔望魔刃 閱讀(372) 評論(1)  編輯  收藏 所屬分類: 設計模式&&數(shù)據(jù)結構

          評論:
          # re: 再理解單例 2010-09-06 12:34 | xylz
          主站蜘蛛池模板: 屯留县| 彰化县| 荆门市| 贵南县| 微山县| 民县| 板桥市| 朝阳市| 诏安县| 札达县| 河源市| 揭阳市| 金坛市| 太仆寺旗| 阜城县| 阿瓦提县| 云龙县| 陇西县| 荆州市| 根河市| 安宁市| 晴隆县| 翼城县| 永州市| 龙江县| 德庆县| 汽车| 炎陵县| 临洮县| 景东| 文水县| 二连浩特市| 农安县| 宜章县| 武汉市| 广德县| 盐城市| 湘乡市| 古交市| 横山县| 云龙县|