In-depth singleton pattern one (transfer)

Reference: http://blog.csdn.net/mrfly/article/details/13372441

 

The singleton pattern is one of the simplest forms of design patterns. The purpose of this pattern is to make an object of a class the only instance in the system. To achieve this, you can start by instantiating it on the client side. Therefore a mechanism is needed that only allows the creation of a unique instance of the object's class, "blocking" all access to the object that is intended to be created. Use factory methods to limit the instantiation process. This method should be a static method (class method) because there is no point in having an instance of the class generate another unique instance.

 

 

 

 

Hungry Chinese code is as follows:

 

[java]  view plain copy  
 
  1. package zhaodp.demo;  
  2.   
  3. publicclass Singleton {   
  4.     privatestatic Singleton uniqueInstance = null;   
  5.     publicstatic Singleton instance(){   
  6.         if(uniqueInstance == null)  
  7.             uniqueInstance = new Singleton();  
  8.         return uniqueInstance;  
  9.     }  
  10. }  

 

Textbook examples of design patterns are generally similar to the code above. If there is a problem with the instance() method in a multi-threaded environment, how can we achieve thread safety? The code can be changed to:

[java]  view plain copy  
 
  1. publicsynchronizedstatic Singleton instance(){    
  2.     if(uniqueInstance == null)  
  3.         uniqueInstance = new Singleton();  
  4.     return uniqueInstance;  
  5. }  

Limiting the instance method with synchronized can indeed solve the problem of thread safety, but it will cause serial execution when multiple threads call the method, which is inefficient. How to improve it? The following code can not only ensure thread safety but also improve the efficiency of multi-threaded concurrency.

[java]  view plain copy  
 
  1. package zhaodp.demo;  
  2.   
  3. publicclass Singleton {   
  4.     privatestatic Singleton uniqueInstance = null;   
  5.   
  6.     publicstatic Singleton instance() {   
  7.         if (uniqueInstance != null)  
  8.             return uniqueInstance;  
  9.         synchronized (Singleton.class) {  
  10.             if (uniqueInstance == null)  
  11.                 uniqueInstance = new Singleton();  
  12.         }  
  13.         return uniqueInstance;  
  14.     }  
  15. }  


 

Or write this:

[java]  view plain  copy
 
  1. package zhaodp.demo;  
  2.   
  3. public class Singleton {  
  4.     private static Singleton uniqueInstance = null;  
  5.   
  6.     public static Singleton instance() {  
  7.         if (uniqueInstance == null) {  
  8.             synchronized (Singleton.class) {  
  9.                 if (uniqueInstance == null)  
  10.                     uniqueInstance = new Singleton();  
  11.             }  
  12.         }  
  13.         return uniqueInstance;  
  14.     }  
  15. }  

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326624785&siteId=291194637