In-depth singleton mode (understanding understanding)

Source: Internet
Author: User

1 /***2 * 1. The simplest implementation3  * 4  * 5  */6 7 /*8 Public class Singletonclass {9 Private Singletonclass () {}Ten private static final Singletonclass instance=new Singletonclass (); One private static Singletonclass getinstance () { A return instance; -     } -  the } - */ - /** -  *  + * 2. Performance optimization -  * +  */ A /*Public class singletonclass{ at Private Singletonclass () {} - private static Singletonclass instance=null; - Public static Singletonclass getinstance () { - if (instance==null) { - instance=new Singletonclass (); -         } in return instance; -     } to }*/ + /*** -  *  the * 3, synchronous, multi-threading *  * $  */Panax Notoginseng /*Public class singletonclass{ - Private Singletonclass () { the     } + private static Singletonclass instance=null; A Public synchronized Static Singletonclass getinstance () { the if (instance==null) { + instance=new Singletonclass (); -         } $ return instance; $          -     } - } the     */ - /***Wuyi * 4. Also performance the  */ - /*Public class singletonclass{ Wu Private Singletonclass () {} - private static Singletonclass instance=null; About Public static Singletonclass getinstance () { $ synchronized (singletonclass.class) { - if (instance==null) { - instance=new Singletonclass (); -             } A         } + return instance; the     } - } $ */ the //double-checked Locking Design the /*Public class singletonclass{ the Private Singletonclass () {} the private static Singletonclass instance=null; - Public Singletonclass getinstance () { in if (instance==null) { the synchronized (singletonclass.class) { the if (instance==null) { About instance=new Singletonclass (); the                 } the             }             the         } + return instance; -     } the }*/Bayi /*** the * 5. Check from source the  */ - /** -  *  the * What steps are required to create a variable? One is to apply a piece of memory, call the constructor method to initialize the operation, and the other is to allocate the * One pointer points to this memory. Which of the two operations who are in front of who in the post? The JVM specification is not specified. So there is a situation, the * The JVM first creates a piece of memory, then points the pointer to the memory, and finally calls the constructor method to initialize it. Let's consider the following. the * In such a situation: Thread A begins to create an instance of Singletonclass, at which point thread B calls the getinstance () method, first - * Determine if instance is null. According to the memory model we mentioned above, A has pointed instance to the memory, just the * The constructor method has not been called, so B detects that instance is not NULL and then returns the instance directly--the problem arises, the * Although instance is not NULL, it is not constructed, like a house has given you the key, but you can't live in it, the * Because the inside hasn't been cleaned up yet. At this point, if B is using this instance before a instance construct is completed, the program will get an error! 94  * the  */ the /*Public class Singletonclass{//wrong job the Private Singletonclass () {}98 private static Singletonclass instance=null; About Public static Singletonclass getinstance () { - if (instance==null) {101 Singletonclass SC;102 synchronized (singletonclass.class) {103 sc=instance;104 if (sc==null) { the synchronized (singletonclass.class) {106 if (sc==null) {107 sc=new Singletonclass ();108                         }109                     } the                 }111 INSTANCE=SC;  the             }113              the         } the return instance; the     }    117 }*/118 119 /*** -  * 121 * 6. Solution122  *123  */124 //after JDK 5, Java uses a new memory model. The volatile keyword has a definite semantics--before JDK1.5, the //volatile is a keyword, but it does not explicitly specify its purpose-the volatile-modified write variable cannot be adjusted to the previous read-write code .126 //read variable can not and after the reading and writing code adjustment! So, as long as we simply add the volatile keyword to the instance. 127 /*Public class singletonclass{ - Private Singletonclass () {}129 private volatile static singletonclass instance=null; the Public static Singletonclass getinstance () {131 if (instance==null) { the synchronized (singletonclass.class) {133 if (instance==null) {134 instance=new Singletonclass ();135                 }            136             }        137         }138 return instance;139     } $ }*/141 /***142 * Recommended Usage143  */144 /*Public class singletonclass{145 Private Singletonclass () {}146 static inner class of//java147 private static Class singletonclassinstance{148 private static final Singletonclass instance=new Singletonclass ();149     } Max Public static Singletonclass getinstance () {151 return singletonclassinstance.instance; the     }    153 }154 */

In-depth singleton mode (understanding understanding)

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.