Discussion on Singleton Mode

Source: Internet
Author: User

The Singleton mode applies in many scenarios, such as database connection pools, logs, and so on. When the system only needs one instance for a class, if two or more instances can be generated, it will only cause troubles and errors. For example, if two programs operate not the same memory, then all the operations of the two programs will not be agreed, or errors will occur during the synchronization process. In the singleton mode, I think from the memory perspective, multiple threads share a piece of memory, and this memory is just a class instance. The simplest way to use the singleton mode is to use the hungry Chinese Singleton mode: [java] public class Single {private static Single current = new Single (); public static Single getSingle () {return current;} private Single () {}} has two key points: 1. in Singleton mode, you need to define the constructor as private, because this prevents the constructor from getting more instances. 2. You need to define a unique instance as a static member variable. Otherwise, you cannot directly obtain the singleton through the class. Why is it called the hunger Singleton mode? I understand that this Singleton has already been created when there is no need for a unique Singleton, it's like you have already prepared your meal when you are not hungry, so we call it the "Hungry Man Singleton" mode. If you are "Hungry", you can "eat" immediately ", otherwise, you will not cook until you are hungry. It is estimated that you will starve to death. The advantage of the hungry Chinese Singleton mode is that it is fast when the singleton mode is required, and there is no need to create it when needed. An instance is already available when the class is created, code is easier to write. The disadvantage is that it slows down the loading speed of classes and affects the system response time (a new instance is required in the initial stage ), A waste of resources (You have to place it in the memory before using this Singleton ). Next, let's take a look at the improvements to the hungry Chinese Singleton mode, that is, the full Chinese Singleton mode: [java] public static Single {private static Single current = null; private Single () {} public static Single getInstance () {if (current = null) {current = new Single ();} return current ;}} this code is okay with a single thread. If multiple threads are accessing the getInstance method, the problem may occur. Multiple Threads judge whether current is null to be true, and then execute current = new Single () in a staggered manner. In this way, multiple instances are generated. The improved method is as follows: [java] public class Single {private static Single current = null; private Single () {} public static synchronized Single getInstance () {if (current = null) {current = new Single ();} return current ;}} although this method can meet the requirements, it has a significant impact on performance, no matter whether the current has been created or not, the synchronized mechanism is used to obtain the current value every time. If the current value is not null, the current value should be returned directly, instead of the synchronization mechanism. So the improvement is as follows: [java] public class Single {private static Single current = null; private Single () {} public static Single getInstance () {if (current = null) {synchronized (Single. class) {current = new Single () ;}} return current ;}} but this writing method also has a problem: multithreading also enters the if (instance = null) Judgment, then, multiple instances are created. Therefore, the Double-checked locking mechanism is required as follows: [java] public class Single {private static Single current = null; private Single () {} public static Single getInstance () {if (current = null) {synchronized (Single. class) {if (current = null) {current = new Single () ;}} return current ;}} this way, the singleton mode is designed, first, we proposed the simplest mode of hungry Chinese Singleton, and then introduced the full Chinese Singleton mode considering its optimization. the evolution process of the full Chinese Singleton mode is as follows: multi-thread concurrency leads to multi-instance problems, performance problems during synchronization, and then multi-thread concurrency leads to multi-instance problems. Finally, Doub is used Le-checked locking.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.