Interview questions: Three modes of single case mode, hungry man, full man, double lock mode, examples and detailed explanation of advantages and disadvantages

The singleton pattern is a very basic design pattern. During the interview, you may be asked to write different types of singleton pattern code. There are three main patterns:

1. Hungry man mode:

 

[java]  view plain copy  
 
  1. //Hungry mode, very hungry and anxious, so the instance object is created when the class is loaded  
  2. public class Singleton1 {  
  3.       
  4.     private static Singleton1 singleton = new Singleton1();  
  5.       
  6.     private Singleton1(){  
  7.           
  8.     }  
  9.       
  10.     public static Singleton1 getInstance(){  
  11.         return singleton;  
  12.     }  
  13. }  

2. Satisfaction mode:

 

[java]  view plain copy  
 
  1. //Satiated Chinese mode, very full and not in a hurry, lazy loading, when to use and when to create an instance, there are thread safety issues  
  2. public class Singleton2 {  
  3.   
  4.     private static Singleton2 singleton;  
  5.       
  6.     private Singleton2(){  
  7.           
  8.     }  
  9.       
  10.     public static synchronized Singleton2 getInstance(){  
  11.         if(singleton == null)   
  12.             singleton = new Singleton2();  
  13.         return singleton;  
  14.     }  
  15. }  

3. Double lock mode:

 

[java]  view plain copy  
 
  1. //Double lock mode of full-man mode to improve efficiency  
  2. public class Singleton3 {  
  3.     private static Singleton3 singleton;  
  4.       
  5.     private Singleton3(){  
  6.           
  7.     }  
  8.       
  9.     public static Singleton3 getInstance(){  
  10.         if(singleton == null){  
  11.             synchronized(Singleton3.class){  
  12.                 if(singleton == null){  
  13.                     singleton = new Singleton3();  
  14.                 }  
  15.             }  
  16.         }  
  17.         return singleton;  
  18.     }  
  19. }  

Comparison of several modes:

1. Hungry mode is thread-safe , because the instance object will be created during the class loading process, and the getInstance() method just returns the object reference directly . The reason why it is called "hungry man" is because it is more "urgent" to create instance objects in this mode, and it is really hungry...

Benefit: Simple and straightforward, no need to worry about thread safety .

Disadvantage: If you use too many singletons in a large environment, you will produce too many instance objects, whether you want to use them or not .

2. The full Chinese mode is not thread-safe , because instance objects are generated when needed. Before production, it will be judged whether the object reference is empty . Here, if multiple threads enter the judgment at the same time, multiple instance objects , which is not in line with the idea of ​​a singleton . Therefore , in order to ensure thread safety , the method is identified with the synchronized keyword . The reason why it is called "full man" is because it is very full, it is not in a hurry to produce instances, it will only produce when it is needed.

Benefits: Delayed loading, objects are only produced when used .

Disadvantages: Need to ensure synchronization, pay the price of efficiency.

3. The double lock mode is an optimization of the satan mode. Double judgment is performed. When the instance object has been created, there is no need to lock it, which improves the efficiency. It is also a recommended way to use

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325358522&siteId=291194637