Categories
Uncategorized

How to Write a singleton pattern performance optimization

Singleton Model interview is among the most common one design pattern, it is an object creation mode, generating a specific example of a subject, the system can ensure that only one instance of a class is generated.

In short, the Singleton pattern can bring two benefits:

1, for the frequent use of the object, the object can save time spent on repetitive creation;

2, to reduce the instance of the operation object, and therefore can reduce the frequency of use of system memory;

 

According to the above two points, it can be seen using a single embodiment mode can effectively improve performance of the system.

 

The most common singleton pattern there is a hungry man and lazy mode.

 

1, so long starving mode:

 

 1 public class Singleton{
 2  
 3   private Singleton(){}
 4   
 5   private static final Singleton instance=new Singleton();
 6 
 7   public static Singleton getInstance(){
 8   
 9   return instance;
10   }
11 }

 

This singleton pattern is very simple, the only downside is, do not delay in loading for instance instance, because the instance is static member variables defined, so when loading JVM singleton, singleton object will be created, if the single when cases like the system also includes other static methods, each time by this singleton class to call other static methods, static member variables will load was defined, which is loaded with private static final Singleton instance = new Singleton (), and therefore it creates a Singleton instance, can be explained through an example:

 

 1 public class Singleton{
 2 
 3     private Singleton(){
 4 
 5         System.out.println("创建了一个单例!");
 6     }
 7 
 8     private static final Singleton instance=new Singleton();
 9 
10     public static Singleton getInstance(){
11 
12         return instance;
13     }
14 
15     public static void Test(){
16 
17         System.out.println("调用了这个方法!");
18     }
19 
20 }

 

 

Print out this information:

 

 

Thus, starving mode because there is no delay loading mechanism, the existence of the object in question can easily be created, which will affect the response speed of the system at the time of call related functions can be added to delay loading mechanism to solve this problem.

 

Plus a singleton pattern of delay mechanism, it becomes our common lazy mode, but this Riga synchronization security mechanisms:

 1 public class SingletonSyn{
 2 
 3 private SingletonSyn(){
 4     System.out.println("创建了一个线程安全的懒汉单例!");
 5 }
 6 
 7     private static  SingletonSyn instance=null;
 8 
 9     public static synchronized SingletonSyn getInstance() {
10     if (instance == null)
11         instance = new SingletonSyn();
12         return instance;
13 
14     }
15 }

 

Place to be noted here that: getInstance () method must be synchronized, or in multi-threaded environment, when the thread 1 n new single embodiment, to complete the operation assignment, the thread 2 may be determined instance is null, so that the thread 2 will start the New singleton program, which would result in multiple instances are created, the impact on performance will intensify, so do add synchronized synchronization is necessary.

Can be described as also Xiao Xiao loser, though coupled with the synchronized keyword synchronization synchronization problem can be solved, but in a multithreaded environment, its performance will be far greater than the first consumed a hungry man mode.

 

Based on the foregoing embodiment two single mode, it can further improvement:

 

 1 public class Singleton{
 2  
 3   private Singleton(){System.out.println("创建了一个基于内部类的单例模式!");}
 4   
 5   private static class SingletonHolder{
 6      private static Singelton instance=new Singleton();
 7 
 8 }
 9 
10   public static  Singleton getInstance(){
11  
12   return SingletonHolder.instance;
13  
14    }
15 }

 

Examples of the above inner class instances to maintain a single embodiment, when Singleton is loaded inside the class will not be initialized, it is possible to ensure that when the class is loaded into the JVM Singleton, singleton class is not initialized, if only getInstance () is called, the class will be loaded inside SingletonHolder, thereby instantiate singleton instance. Meanwhile, since the establishment of the instance is done only when the class is loaded, it is inherently multi-threaded friendship, getInstance methods without the use of synchronous synchronized, visible, using the internal class way to achieve a singleton, lazy loading can be done both, do not use synchronized keyword, is a more complete realization.

 

Of course, Singleton For a more complete design, there are better ways, interested partners can continue to carry out a thorough discussion.

 

My blog is about to be synchronized to Tencent cloud + community, inviting all of them settled: https:? //Cloud.tencent.com/developer/support-plan invite_code = 2lfj12z61ny88

Leave a Reply