Singleton design pattern-(Are you sure you're writing a lazy singleton that's really thread-safe?)

Source: Internet
Author: User

1. Advantages and disadvantages of single case design pattern

Advantages:

1): Create only one instance, and you can use it everywhere to speed up the efficiency of creating entities

Disadvantages:

1): If the frequency of use is relatively low, the instance will always occupy the memory space, will cause the waste of resources

2): Thread safety issues may occur

2. Two kinds of definite method of single case design pattern (a hungry man, lazy)

A Hungry Man method: ( may cause waste of resources, class one is loaded to create an instance, but does not ensure that the instance will be used )

Package Com.zluo.pattern;/** * * project name: Single-instance <br> * class Name: singleinstance_01 <br> * Class Description: A hungry man type <br> * Creator: Louzhangjie <br> * Creation time: May 23, 2015 PM 1:55:53 <br> modified by: Louzhangjie <br> * Modified: May 23, 2015 PM 1:55:53 <br> * Edit Note: <br> * @version 1.0 <br> **/ Public classsingleinstance_01 {Private Staticsingleinstance_01 instance =Newsingleinstance_01 (); Privatesingleinstance_01 () {} Public Staticsingleinstance_01 getinstance () {returninstance; }}

For a hungry man, it does not pose a thread-safety issue, and it can be a thread-safe issue for lazy writing in multithreaded situations, primarily because multiple instances may be created when an instance is created

General wording: (The following wording seems to have no problem, but is it really thread-safe?) )

 Public Staticsingleinstance_02 Getinstancewiththreadsafe () {Try{         if(Instance = =NULL){
A Thread.Sleep (1000); synchronized("") {instance=Newsingleinstance_02 (); } } returninstance; }Catch(Exception e) {e.printstacktrace (); return NULL; }}

  (In fact, there is still a thread-safety problem with the above, because when there are two threads entering a, even if the entity class is created with a lock, it still creates multiple instances, and the instance created by the previous thread has no effect on the thread that has already entered a. This is also a loophole in this writing.

After the optimization of the wording:

 Public Staticsingleinstance_02 Getinstancewithoutthreadsafe () {Try{        if(Instance = =NULL){            //let's say thread 1, threads 2 wait hereThread.Sleep (1000); synchronized("") {                if(Instance = =NULL){                    //One more time to determine if it is null to prevent the occurrence of multiple lines waiting for the lockInstance =Newsingleinstance_02 (); }            }        }    }Catch(Exception e) {e.printstacktrace (); }    returninstance;}

If you have any questions or suggestions, please leave a message, I will complete as soon as possible, grateful

Download code:)

Singleton design pattern-(Are you sure you're writing a lazy singleton that's really thread-safe?)

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.