Design Mode-single-piece Mode

Source: Internet
Author: User

In the project, we only need one object, such as the thread pool, cache, and dialog box. The common practice is to set a global static variable, then they are instantiated during program initialization, and then the global variable is called directly. However, if this problem occurs, if my object consumes a lot of resources, sometimes, I didn't use this object during the running process of my program. I am not wasting a lot of resources. The common practice is to instantiate a Global static variable during the call process instead of initializing it, we need to determine whether the variable has been instantiated. If it is instantiated, we will not instantiate it, so the code is as follows:

public class A{    private A(){}    public static A cla;    public static A getInstance()    {        if (cla == null){            cla=new A();        }        return cla;    }}

Here, the constructor of A is private, which can effectively ensure that Class A will not be instantiated externally, so as to ensure its uniqueness, we can use getinstance to obtain the globally unique variable of A. This effectively solves the problem above, but the problem arises again. When multiple threads access getinstance? What should I do if the content of the object is instantiated after it is instantiated once?

public class A{    private A(){}    public static A cla;    public static synchronized A getInstance()    {        if (cla == null){            cla=new A();        }        return cla;    }}

We add a synchronization lock for the method getinstance for a to obtain the strength to ensure that the method can only be accessed uniquely. However, the synchronization lock will inevitably reduce the running efficiency, and we find that, in fact, that is, the synchronization lock is required for the first instantiation, and it is not required to obtain the object later. If we frequently obtain the object, efficiency will be greatly affected. Back to the initial practice? Set global variables to instantiate objects when the program is running?

public class A{    private A(){}    public static A cla = new A();    public static synchronized A getInstance()    {        return cla;     }}

This is indeed a method, but it is always not the best method. We think that, as mentioned earlier, only synchronization locks are required during instantiation, then we thought of using the double check lock method, only locking during instantiation:

public class A{    private A(){}    public volatile static A cla;    public static A getInstance()    {         if (cla == null){            synchronized(A.class){                if (cla==null){                    cla=new A();                }            }          }        return cla;    }}

Among them, the volatile keyword is to ensure that when the multi-thread class is initialized, the class a variable is correctly processed. (Volatile is used in multithreading to synchronize variables. To improve the efficiency, the thread copies a member variable (such as a) (such as B), and the access to a in the thread is actually B. Only A and B are synchronized in some actions. Therefore, A and B are inconsistent. Volatile is used to avoid this situation. Volatile tells JVM that the modified variables do not keep copies and directly access the primary memory (that is, a mentioned above ))

Design Mode-single-piece Mode

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.