Understanding of the role of Java Singleton mode volatile keywords

Source: Internet
Author: User
Tags volatile

Single-case mode is often used in programming, simple and convenient design mode, but also a lot of program ape on the design of the first class of the introduction of the model. One of the most classic of these is:

Class Singleton {    private volatile static Singleton instance;//volatile keyword prevents the command from being re    -ordered private Singleton () {}    public static Singleton getinstance () {        if (instance = = NULL) {//One re-judgment            synchronized (singleton.class) {//Pair single Ton.class Lock                if (instance = = NULL) {//double judgment                    instance = new Singleton ();}}        }        return instance;}    }

There are two key places: 1, initialize the instance instance of the appropriate, using double judgment on the Singleton.class lock.

2, static variable instance is decorated with the volatile keyword.

The first is a relatively good understanding.

        if (instance = = NULL) {//One revaluation            synchronized (singleton.class) {//Singleton.class lock                if (instance = = NULL) {//Two Re                    -judge instance = new Singleton ();}}}        

  

sychronized keyword locks Singleton class instead of instance object because, At this time instance is empty, add sychronized no meaning.
The significance of the first judgment is that if the instance is not empty, the steps of the lock are skipped, reducing the resource overhead of the lock. However, because the first judgment is outside the code lock, if different threads access the Install==null at the same time, the content inside the code lock will be blocked successively. Therefore, in the code lock Nega second judgment is necessary, to avoid the first thread to get the instance, the second thread acquires a resource lock and performs a instance initialization, resulting in two different instances.

Span style= "font-size:12px" > https://www.cnblogs.com/shan1393/p/8999683.html
https://www.cnblogs.com/xrq730/p/7048693.html

Instance = new Singleton () appears to have only one sentence, but the initialization member variables of Java include memory allocation, initialization, a series of operations that return the object's reference on the heap, and so on. There is no problem with full execution of a statement within a synchronized code block. However, if a command reflow occurs, the object's reference on the heap is first returned and then initialized. For another thread that executes to a single judgment, because instance has pointed to a memory space on the heap, instance is not empty and returns an uninitialized object to other function calls, causing a series of unsafe pitfalls. The volatile keyword is added to ensure that the return object's reference on the alignment occurs after initialization, preventing this from happening. This is the role of the instance plus volatile keyword in singleton mode.

Understanding of the role of Java Singleton mode volatile keywords

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.