How to write a singleton pattern for performance optimization

Singleton model is the most common design pattern in interview. It is an object creation pattern used to generate a specific instance of an object, which can ensure that only one instance is generated for a class in the system.

In short, singleton mode can bring two benefits:

1. For frequently used objects, you can save the time spent on repeatedly creating objects;

2. Reduce the instantiation of objects, so it can reduce the use frequency of system memory;

According to the above two points, it can be seen that using singleton mode can effectively improve the performance of the system.

The most common singleton modes are hungry man mode and lazy man mode.

1. The hungry man model looks like this:

This singleton mode is very simple. The only disadvantage is that it is impossible to delay loading the instance. Because the instance member variable is defined by static, the singleton object will be established when the JVM loads the singleton class. If this singleton class also contains other static methods in the system, every time you call other static methods through this singleton class, The member variables defined by static will be loaded, that is, private static final singleton instance = new singleton(), so a singleton instance will be created, which can be explained by an example:

Print out such information:

It can be seen that the starving Han mode has the problem that objects are easy to be created because there is no delayed loading mechanism, which will affect the reaction speed of the system when calling related functions. The delayed loading mechanism can be added to solve this problem.

The single instance mode with delay mechanism has become a common lazy mode, but the synchronous security mechanism is added here:

Note that the getInstance () method must be synchronized. Otherwise, in a multithreaded environment, when thread 1 is creating a new instance and completes the operation assignment, thread 2 may judge that the instance is null, so thread 2 will also start the new instance program, which will lead to the creation of multiple instances and aggravate the impact on performance, Therefore, it is necessary to add synchronized for synchronization.

Although the synchronization keyword synchronized can solve the synchronization problem, its performance consumption will be much greater than that of the first hungry man mode in a multi-threaded environment.

Based on the previous two singleton modes, it can be further improved:

The above example uses the internal class to maintain the instance of the singleton. When the singleton is loaded, its internal class will not be initialized. Therefore, it can be ensured that when the singleton class is loaded into the JVM, the singleton class will not be initialized. Only when getinstance() is called, the internal class singletonholder will be loaded to instantiate the singleton class instance. At the same time, since the instance is created only when the class is loaded, it is naturally multi-threaded friendly, and the getInstance method does not need to use synchronous synchronized. It can be seen that using the internal class method to implement a single instance can not only delay loading, but also do not need to use synchronous keywords. It is a relatively perfect implementation.

Of course, if you need to improve the design of singleton mode, there are better ways. Interested partners can continue to have an in-depth discussion.

My blog is about to be synchronized to Tencent cloud + community. I invite you to join me: https://cloud.tencent.com/developer/support-plan?invite_code=2lfj12z61ny88

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>