Double lock principle of single case mode

Source: Internet
Author: User
Why do you double lock when you create a single case pattern in multiple threads? Review the double locked blocks of code first.

public class SingleTon {
   
    private static SingleTon SingleTon = null;
   
       
    Public SingleTon () {
        //TODO auto-generated constructor stub
    } public
   
   
    static SingleTon getinstance () {
        if (SingleTon = = null) {
            synchronized (singleton.class) {
                if (SingleTon = = null) {
                    SingleTon = new SingleTon () ;
                }
            }
        }
        Return SingleTon
    }

}


Why use double check locking? I've probably said that earlier.

Consider a situation where two threads arrive at the same time, calling the GetInstance () method at the same time.

At this point because of singleTon = = NULL, it is obvious that two threads can pass the first heavy singleTon = = NULL,

Once the first heavy if statement is entered, a thread enters the lock statement and enters the second heavy singleTon = null, because of the locking mechanism.

Another thread waits outside the lock statement.

When the first thread finishes executing the new SingleTon () statement, it exits the locked region, at which point the second thread can enter the lock statement block.

At this point, if there is no second heavy singleTon = = NULL, then the second thread can still invoke the new SingleTon () statement,

The second thread would also create a singleton instance, which would also violate the original intent of the singleton pattern,

So here you have to use a double check lock.

Careful friend will find that if I remove the first heavy singleton = = NULL, the program can still run in a multithreaded,

Considering that without the first heavy singleton = = NULL,

When two threads arrive at the same time, because of the lock mechanism, the first thread enters the lock statement block and can successfully execute the new SingleTon ().

When the first thread exits the lock statement block, the SingleTon static variable is no longer null, so when the second thread enters lock, the

Or will be the second heavy singleton = = null block outside, and cannot execute the new singleton (),

therefore, in the absence of the first Singleton = = NULL, it is also possible to implement the single example mode. So why do we need the first heavy singleton = = null?

Here's a performance issue, because the new SingleTon () only needs to be executed once for the single case mode.

And if there is no first heavy singleTon = null, every time a thread enters getinstance (), a lock operation is performed to achieve the synchronization of threads.

This is very performance-intensive, and if I add the first heavy SingleTon = = NULL,

Then it is only when the first, the Singletton ==null is established, that a lock is performed to achieve thread synchronization,

In the future, it is OK to simply return to the Singleton instance and no longer need to enter the lock statement block, which can solve the performance problems caused by thread synchronization.

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.