About Java single case

Source: Internet
Author: User

Reference: http://blog.csdn.net/haoel/article/details/4028232

 Public classSingletontestImplementsRunnable {StaticSingletonClass1 instance =NULL;  Public Static voidMain (String args[]) {//SINGLETONCLASS0 instance = new SingletonClass0 ();//The constructor//SingletonClass0 () is not visible         for(inti = 0; I < 15; i++) {            NewThread (Newsingletontest ()). Start (); }        //System.out.println (Instance.getclass ());} @Override Public voidrun () {//TODO auto-generated Method Stubsingletonclass6.getinstance (); //System.out.println (SingletonClass7.instance.hashCode ());    }}classSingletonClass0 {Private StaticSingletonClass0 instance =NULL; PrivateSingletonClass0 () {System.out.println ("Constructor Run"); }     Public StaticSingletonClass0 getinstance () {//wrong single thread is not a problem, there will still be multiple instances under multithreading        if(Instance = =NULL) {instance=NewSingletonClass0 ();        System.out.println (Instance.hashcode ()); }        returninstance; }}classSingletonClass1 {Private StaticSingletonClass1 instance =NULL; PrivateSingletonClass1 () {System.out.println ("Constructor Run"); }     Public StaticSingletonClass1 getinstance () {//There will still be multiple instances under wrong multithreading, with the previous//the difference is that the new process of having a thread is synchronized.        if(Instance = =NULL) {            synchronized(SingletonClass1.class) {instance=NewSingletonClass1 ();            System.out.println (Instance.hashcode ()); }        }        returninstance; }}classSingletonClass2 {Private StaticSingletonClass2 instance =NULL; PrivateSingletonClass2 () {System.out.println ("Constructor Run"); }     Public StaticSingletonClass2 getinstance () {synchronized(SingletonClass2.class) {//Right . There will only be one thread new instance, but the subsequent thread is blocked from reading the instance            if(Instance = =NULL) {instance=NewSingletonClass2 ();            System.out.println (Instance.hashcode ()); }        }        returninstance; }}classSINGLETONCLASS3 {Private StaticSINGLETONCLASS3 instance =NULL; PrivateSingletonClass3 () {System.out.println ("Constructor Run"); }     Public StaticSINGLETONCLASS3 getinstance () {synchronized(SINGLETONCLASS3.class) {// Right//there will only be one thread new instance that will not affect subsequent thread read instances, but Instance=new//SINGLETONCLASS3 () is not an atomic operation within the JVM, and several steps inside may be out of order, resulting in an error            if(Instance = =NULL) {                synchronized(SingletonClass1.class) {                    if(Instance = =NULL) {instance=NewSINGLETONCLASS3 ();                    System.out.println (Instance.hashcode ()); }                }            }        }        returninstance; }}classSINGLETONCLASS4 {Private Static volatileSINGLETONCLASS4 instance =NULL;// Right//with only one instance, Volatite guarantees that the new inside the JVM//Several operations of instance prohibit command reordering optimization    PrivateSingletonClass4 () {System.out.println ("Constructor Run"); }     Public StaticSingletonClass4 getinstance () {synchronized(SINGLETONCLASS4.class) {            if(Instance = =NULL) {                synchronized(SingletonClass1.class) {                    if(Instance = =NULL) {instance=NewSingletonClass4 ();                    System.out.println (Instance.hashcode ()); }                }            }        }        returninstance; }}classSINGLETONCLASS5 {//right, but when the class loader creates an instance when the class loads, we can't control when the instance is created to do something (like a configuration file or a resource created by another class)     Public volatile StaticSINGLETONCLASS5 instance =NewSingletonClass5 (); PrivateSingletonClass5 () {System.out.println ("Constructor Run");    };  Public StaticSingletonClass5 getinstance () {System.out.println (Instance.hashcode ()); returninstance; }}classSINGLETONCLASS6 {//The JVM itself is still used to ensure thread safety, since Singletonholder is private, except//getinstance ()//There is no way to access it, so it is only actually created when getinstance () is called, while the instance is read without synchronization, no performance flaw, and no dependency//JDK version    Private Static classSingletonholder {Private Static FinalSINGLETONCLASS6 INSTANCE =NewSingletonClass6 (); }    PrivateSingletonClass6 () {System.out.println ("Constructor Run"); }     Public Static FinalSingletonClass6 getinstance () {System.out.println (SingletonHolder.INSTANCE.hashCode ()); returnsingletonholder.instance; }}classSINGLETONCLASS7 { PublicSingletonClass7 () {System.out.println ("Constructor Run"); }}enumSingletonclass {instance;}
View Code

About Java Singleton

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.