Singleton pattern of Java design pattern

Singleton pattern is also called single shoulder pattern. It is also a creation pattern. It is the most commonly used design pattern in our daily development. It is often used to encapsulate some tool classes, such as database connection.

Definition of singleton mode:

Singleton pattern is a common software design pattern. Its core structure contains only a special class called singleton. The singleton pattern ensures that there is only one instance of a class in the system. That is, a class has only one object instance

Single case mode is divided into the following types: ① hungry man single case mode ② lazy man single case mode ③ IODH single case mode ④ enumeration single case mode

Let's introduce them one by one:

1. The first is the hungry man singleton mode. As the name suggests, "hungry man" means "hungry man" in a hurry to "eat" (the meal here should refer to the instance object), so it was created very early. Not much to say, the code:

This code vividly reflects the hungry man mode.

This method is instantiated when the class is loaded (only create a unique instance), so there are no thread problems. However, because of this, if there are too many single instances and these instances are not often used for so long, it will cause a waste of memory.

2. Lazy mode, what is also called lazy mode? A lazy person will wash the dishes only when he goes to eat (the dirty bowl used yesterday), that is, he will create objects only when he uses objects and write code:

The above code is lazy mode. When we don't need an instance, it won't create an instance. It will only be created when we need an instance to save space. However, we do not recommend it, Because our original design concept is: when needed (and instance is empty) create an instance. If an instance is needed after it has been created, judge whether instance is empty. Because the previous instance has been created, it is not empty, so directly return the previously created instance. The above idea is good, but it is thread unsafe. Because: when multiple threads call this method at the same time, because Judge that the instance is empty, so many instances will be created, which is not in line with the design concept of singleton pattern (a class has only one instance object).

Therefore, we need to improve it because it will create multiple instances asynchronously, so we can easily think of adding synchronized before getInstance method for synchronization:

In this way, multiple instance objects will not be created, which conforms to the concept of singleton mode.

However, the above design makes us queue up every time we need the target instance. Assuming that many threads are calling this getInstance method, we will fall into a long wait, which greatly reduces the efficiency of the program.

So we have to make some modifications to the program if we create one

The above method is the complete version of lazy mode. Let's analyze it carefully:

When multiple threads call this method for the first time, they all meet that instance is empty. Go to the next step. The thread that obtains the lock first goes to the next step to continue to judge whether it is empty or not, and then create an instance to return the instance.

The thread that has not obtained the lock waits until the thread that has obtained the lock releases the lock. When the waiting thread obtains the lock, it goes to the next step to judge whether the instance is empty. However, the instance is not empty at this time. Because the previous thread has created an instance, it returns the created instance.

The above is the first batch of threads calling the getInstance method. When subsequent threads call this method, there is no need to obtain and release the lock, because the first step has determined that the instance is not empty and directly returns the instance.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

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
分享
二维码
< <上一篇
下一篇>>