單例 (JAVA)

來源:互聯網
上載者:User

標籤:oid   懶漢   row   nal   class   懶漢式   ==   nts   protected   

 java中單例模式是一種常見的設計模式,以下是它的特點:

 

  • 單例類只能有一個執行個體。
  • 單例類必須自己建立自己的唯一執行個體。
  • 單例類必須給所有其他對象提供這一執行個體

 

第一種(懶漢,線程不安全):

 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 

 

 

  懶漢式是典型的時間換空間,就是每次擷取執行個體都會進行判斷,看是否需要建立執行個體,浪費判斷的時間。當然,如果一直沒有人使用的話,那就不會建立執行個體,則節約記憶體空間

 由於懶漢式的實現是安全執行緒的,這樣會降低整個訪問的速度,而且每次都要判斷.

第二種(懶漢,安全執行緒):

 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 


第三種(餓漢):

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 

 

第四種(餓漢,變種): 

 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 

優點:載入時不會初始化靜態變數INSTANCE,因為沒有主動使用,達到Lazy loading

 

 

第六種(枚舉):

 

1 public enum Singleton {  
2     INSTANCE;  
3     public void whateverMethod() {  
4     }  
5 }  
6 

優點:不僅能避免多線程同步問題,而且還能防止還原序列化

 

第七種(雙重校正鎖):

 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 

總結

有兩個問題需要注意:

     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 

單例 (JAVA)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.