<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                出自 > [Java:單例模式的七種寫法](http://www.blogjava.net/kenzhh/archive/2013/03/15/357824.html) [TOC=1,2] **第一種(懶漢,線程不安全):** ?1?public?class?Singleton?{?? ?2?????private?static?Singleton?instance;?? ?3?????private?Singleton?(){}??? ?4?????public?static?Singleton?getInstance()?{?? ?5?????if?(instance?==?null)?{?? ?6?????????instance?=?new?Singleton();?? ?7?????}?? ?8?????return?instance;?? ?9?????}?? 10?}?? 11? 這種寫法lazy loading很明顯,但是致命的是在多線程不能正常工作。 **第二種(懶漢,線程安全): ** ?1?public?class?Singleton?{?? ?2?????private?static?Singleton?instance;?? ?3?????private?Singleton?(){} ?4?????public?static?synchronized?Singleton?getInstance()?{?? ?5?????if?(instance?==?null)?{?? ?6?????????instance?=?new?Singleton();?? ?7?????}?? ?8?????return?instance;?? ?9?????}?? 10?}?? 11? 這種寫法能夠在多線程中很好的工作,而且看起來它也具備很好的lazy loading,但是,遺憾的是,效率很低,99%情況下不需要同步。 **第三種(餓漢):** 1?public?class?Singleton?{?? 2?????private?static?Singleton?instance?=?new?Singleton();?? 3?????private?Singleton?(){} 4?????public?static?Singleton?getInstance()?{?? 5?????return?instance;?? 6?????}?? 7?}?? 8? 這種方式基于classloder機制避免了多線程的同步問題,不過,instance在類裝載時就實例化,雖然導致類裝載的原因有很多種,在單例模式中大多數都是調用getInstance方法,?但是也不能確定有其他的方式(或者其他的靜態方法)導致類裝載,這時候初始化instance顯然沒有達到lazy loading的效果。 **第四種(**餓**漢,變種):** ?1?public?class?Singleton?{?? ?2?????private?Singleton?instance?=?null;?? ?3?????static?{?? ?4?????instance?=?new?Singleton();?? ?5?????}?? ?6?????private?Singleton?(){} ?7?????public?static?Singleton?getInstance()?{?? ?8?????return?this.instance;?? ?9?????}?? 10?}?? 11? 表面上看起來差別挺大,其實更第三種方式差不多,都是在類初始化即實例化instance。 **第五種(靜態內部類): ** ?1?public?class?Singleton?{?? ?2?????private?static?class?SingletonHolder?{?? ?3?????private?static?final?Singleton?INSTANCE?=?new?Singleton();?? ?4?????}?? ?5?????private?Singleton?(){} ?6?????public?static?final?Singleton?getInstance()?{?? ?7?????????return?SingletonHolder.INSTANCE;?? ?8?????}?? ?9?}?? 10? 這種方式同樣利用了classloder的機制來保證初始化instance時只有一個線程,它跟第三種和第四種方式不同的是(很細微的差別):第三種和第四種方式是只要Singleton類被裝載了,那么instance就會被實例化(沒有達到lazy loading效果),而這種方式是Singleton類被裝載了,instance不一定被初始化。因為SingletonHolder類沒有被主動使用,只有顯示通過調用getInstance方法時,才會顯示裝載SingletonHolder類,從而實例化instance。想象一下,如果實例化instance很消耗資源,我想讓他延遲加載,另外一方面,我不希望在Singleton類加載時就實例化,因為我不能確保Singleton類還可能在其他的地方被主動使用從而被加載,那么這個時候實例化instance顯然是不合適的。這個時候,這種方式相比第三和第四種方式就顯得很合理。 **第六種(枚舉):** 1?public?enum?Singleton?{?? 2?????INSTANCE;?? 3?????public?void?whateverMethod()?{?? 4?????}?? 5?}?? 6? 這種方式是Effective Java作者Josh Bloch 提倡的方式,它不僅能避免多線程同步問題,而且還能防止反序列化重新創建新的對象,可謂是很堅強的壁壘啊,不過,個人認為由于1.5中才加入enum特性,用這種方式寫不免讓人感覺生疏,在實際工作中,我也很少看見有人這么寫過。 **第七種(雙重校驗鎖): ** ?1?public?class?Singleton?{?? ?2?????private?volatile?static?Singleton?singleton;?? ?3?????private?Singleton?(){}??? ?4?????public?static?Singleton?getSingleton()?{?? ?5?????if?(singleton?==?null)?{?? ?6?????????synchronized?(Singleton.class)?{?? ?7?????????if?(singleton?==?null)?{?? ?8?????????????singleton?=?new?Singleton();?? ?9?????????}?? 10?????????}?? 11?????}?? 12?????return?singleton;?? 13?????}?? 14?}?? 15? 這個是第二種方式的升級版,俗稱雙重檢查鎖定,詳細介紹請查看:[http://www.ibm.com/developerworks/cn/java/j-dcl.html](http://www.ibm.com/developerworks/cn/java/j-dcl.html) 在JDK1.5之后,雙重檢查鎖定才能夠正常達到單例效果。 **總結** 有兩個問題需要注意: ???? 1、如果單例由不同的類裝載器裝入,那便有可能存在多個單例類的實例。假定不是遠端存取,例如一些servlet容器對每個servlet使用完全不同的類? 裝載器,這樣的話如果有兩個servlet訪問一個單例類,它們就都會有各自的實例。 ???? 2、如果Singleton實現了java.io.Serializable接口,那么這個類的實例就可能被序列化和復原。不管怎樣,如果你序列化一個單例類的對象,接下來復原多個那個對象,那你就會有多個單例類的實例。 對第一個問題修復的辦法是: ?1?private?static?Class?getClass(String?classname)?????? ?2??????????????????????????????????????????throws?ClassNotFoundException?{????? ?3???????ClassLoader?classLoader?=?Thread.currentThread().getContextClassLoader();????? ?4??????? ?5???????if(classLoader?==?null)????? ?6??????????classLoader?=?Singleton.class.getClassLoader();????? ?7??????? ?8???????return?(classLoader.loadClass(classname));????? ?9????}????? 10?}?? 11? ?對第二個問題修復的辦法是:? ?1?public?class?Singleton?implements?java.io.Serializable?{????? ?2????public?static?Singleton?INSTANCE?=?new?Singleton();????? ?3??????? ?4????protected?Singleton()?{????? ?5????????? ?6????}????? ?7????private?Object?readResolve()?{????? ?8?????????????return?INSTANCE;????? ?9???????}???? 10?}??? 11? 對我來說,我比較喜歡第三種和第五種方式,簡單易懂,而且在JVM層實現了線程安全(如果不是多個類加載器環境),一般的情況下,我會使用第三種方式,只有在要明確實現lazy loading效果時才會使用第五種方式,另外,如果涉及到反序列化創建對象時我會試著使用枚舉的方式來實現單例,不過,我一直會保證我的程序是線程安全的,而且我永遠不會使用第一種和第二種方式,如果有其他特殊的需求,我可能會使用第七種方式,畢竟,JDK1.5已經沒有雙重檢查鎖定的問題了。 ======================================================================== ?[superheizai](http://superheizai.javaeye.com/)同學總結的很到位: 不過一般來說,第一種不算單例,第四種和第三種就是一種,如果算的話,第五種也可以分開寫了。所以說,一般單例都是五種寫法。懶漢,惡漢,雙重校驗鎖,枚舉和靜態內部類。 我很高興有這樣的讀者,一起共勉。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看