Optimization of Singleton mode analysis code

Source: Internet
Author: User

Optimization of Singleton mode analysis code
The Singleton mode is one of the 23 design patterns and is a relatively simple design pattern. It aims to return the same object no matter how many calls, it features privatization of constructors. It can be divided into two structures: lazy and ELE. Me. They have their own advantages and disadvantages. Let's start with the following code: public class Single {public static Single single = new Single (); private Single () {}public Single getInstance () {return single ;}} the above program shows that although the goal of loading the same object is indeed achieved, the single object will be created when the program is loaded, when this class has multiple such methods, we may not use most of the single examples of this object, which will cause a waste of memory. So there is a lazy Singleton mode. The Code is as follows: public class Single {public static Single single = null; private Single () {} public Single getInstance () {if (single = null) {return new Single () ;}return single ;}} this way, it will be new only when we actually call this object, but there is a problem. When two threads call the second part of the code during the first loading, two different objects will be generated, so the thread is not secure, at this time, we will think of adding a lock to this method. The code after the lock is as follows: public class Single {public static Single single = null; private Single () {} public synchronized Single getInstance () {if (single = null) {return new Single () ;}return single ;}} does ensure thread security, but when locking this method, many things need to be executed, it takes a long time to call this method, which is fatal to the server, because if a thread keeps calling this method, other threads cannot call it, the server is blocked. The upgraded code is as follows: public class Single {Public static Single single = null; private Single () {} public Single getInstance () {if (single = null) {synchronized (Single. class) {return new Single () ;}} return single ;}after careful observation, it is found that this is not locked. When two threads reach the getInstance () method if for the first time, one of them must be blocked. After another thread is executed, blocking the thread will no longer judge whether it is empty or create an object, in this way, multiple objects are generated and upgraded. The Code is as follows: public class Single {public static Single single Single = null; private Single () {} public Single getInstance () {if (single = null) {synchronized (Single. class) {if (single = null) {return new Single () ;}} return single ;}} this will not cause the above problem, and it will only be locked once, when this method is re-executed for the second time, if judgment will be skipped and single will be returned directly without being locked, resulting in high execution efficiency. But even so, there is still a problem, because we are not sure whether to assign a value to the object in the memory or create the object first, therefore, the second program may get half of the initialization object. After jdk1.5, we can use the volatile keyword to avoid this situation. The Code is as follows: public class Single {public static volatile Single single = null; private Single () {} public Single getInstance () {if (single = null) {synchronized (Single. class) {if (single = null) {return new Single () ;}} return single ;}} but this situation is rarely used. d I am only here to learn, xi

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.