Java Lazy Singleton encounters multi-threading

Source: Internet
Author: User

Singleton mode ensures that a class has only one instance, and instantiates itself and provides this instance to the entire system.

In a computer system, the driver objects of the thread pool, cache, log Object, dialog box, printer, and video card are often designed as singleton. These apps have more or less the functionality of the resource manager.

Each computer can have several printers, but only one printer Spooler to prevent both print jobs from being output to the printer at the same time. Each computer can have several communication ports, and the system should centrally manage these communication ports to prevent a communication port from being called simultaneously by two requests. In short, the selection of Singleton mode is to avoid inconsistent state, to avoid long-running political.

1  Public classSingletonthreadImplementsRunnable {2     PrivateUser U =NULL;3 4      Public Static voidMain (string[] args)throwsinterruptedexception {5          for(inti=0;i<1000;i++){6Singletonthread T =NewSingletonthread ();7Singletonthread T1 =NewSingletonthread ();8Thread t3 =NewThread (t);9Thread T4 =NewThread (t1);Ten T3.start (); One T4.start (); AT3.join (0); -T4.join (0); -             if(T.getuser ()! =T1.getuser ()) theSYSTEM.OUT.PRINTLN ("Multithreading causes single-instance creation failure ..."); -         } -          -     } +      PublicUser GetUser () { -         returnu; +     } A  at @Override -      Public voidrun () { -         Try { -U =user.getinstance (); -}Catch(interruptedexception e) { -             //TODO auto-generated Catch block in e.printstacktrace (); -         } to     } +     /* - * Lazy One-thread insecure the      * */ *      Public Static classUser { $         PrivateUser () {Panax Notoginseng         } -  the         Private StaticUser U =NULL; +  A          Public StaticUser getinstance ()throwsinterruptedexception { the             if(U = =NULL) { +Thread.CurrentThread (). Sleep (1000); -System.out.println ("ThreadId:" +Thread.CurrentThread (). GetId ()); $U =NewUser (); $             } -             returnu; -         } the     } -     /*Wuyi * Chinese-style single case-thread safety the      *  -      * */ Wu      Public Static classUser1 { -         PrivateUser1 () {} About         Private StaticUser1 U =NewUser1 (); $  -          Public StaticUser1 getinstance ()throwsinterruptedexception { -Thread.CurrentThread (). Sleep (1000); -             returnu; A         } +     } the  -}

Java Lazy Singleton encounters multi-threading

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.