[design mode] Singleton mode (consider multithreading)

Source: Internet
Author: User

The following program is written by the teacher, more classic, now forget. Save them first.

1 Importjava.util.ArrayList;2 3  Public Final classSingletonobject {4     Private StaticSingletonobject obj;5     PrivateSingletonobject () {}6     //If you declare synchronization on a method, it becomes a bottleneck for system concurrent access7      Public Staticsingletonobject Getinst () {8         if(obj = =NULL){9             synchronized("AA"){Ten                 if(obj = =NULL){ Oneobj =NewSingletonobject (); A                 } -             } -         } the         returnobj; -     } -  -     //1. Declare an object into a class as a class variable, initialize +     //violated the first article. -     //2.1. Declaring an object in a class as a class variable and not initializing +     //initializing in Static A     //always have an initialization code, where to write? If you write to the static code block, then the class is loaded, it is executed, and the direct write Obj2 = .... The same at     //The above scenario violates the first article below -     //2.2. Declaring an object in a class as a class variable and not initializing -     //writes initialization to the method that gets the object -     //obj2 = new Singletonobject (); -     //The above scenario violates the following second article -     //2.2.1. Declaring an object into a class as a class variable and not initializing in     //The decision to NULL in the get method is initialized -     //The above scenario violates the following article III to     //Final scenario: Both the initialization of the object and the initialization statement cannot be put into the Fetch method +     //which scenario in Java belongs to the model system of a class and executes only once, and does not load when the class is loaded, but only when it is loaded, then this scenario is the final scenario -     //Internal class Solutions the      *     Private Static classinner{ $         //adding an object initialization action to an inner classPanax Notoginseng         Private Static FinalSingletonobject Obj2 =NewSingletonobject (); -     } the      +      Public Staticsingletonobject GetInst2 () { A         //both require lazy loading, do not use not create the         //but also to ensure that the creation of the time will not be created multiple times to create a single case +         //It 's best not to make a null decision every time you get it. -         returnInner.obj2; $     } $      -      Public Static voidmain2 (string[] args) { - System.out.println (Singletonobject.getinst ()); the System.out.println (Singletonobject.getinst ()); - System.out.println (Singletonobject.getinst ());Wuyi     } the      -      Public Static voidMain (string[] args) { Wu          -         LongT1 =System.currenttimemillis (); About         LongStart = 90000000000000001L; $         LongEnd = 90000000000000100L; -         LongRefmax = (Long) math.sqrt (end); -Arraylist<long> primes =NewArraylist<long>(); - Out : A          for(Longi = 2;i <= Refmax; i++){ +              for(Longj = 2;j<= math.sqrt (i); J + +){ the                 if(i% j = 0){ -                     ContinueOut ; $                 } the             } the Primes.add (i); the         } theSystem.out.println ("End"); - Out : in          for(Longi = start;i<=end;i++){ the              for(Long prime:primes) { the                 if(i% prime = = 0){ About                     ContinueOut ; the                 } the             } theSystem.out.print (i+ "\ T"); +         } -         Longt2 =System.currenttimemillis (); theSystem.out.println (t2-t1);Bayi          the          the Out : -          for(inti = 101;i <= 200; i++){ -              for(intj = 2;j<= math.sqrt (i); J + +){ the                 if(i% j = 0){ the                     ContinueOut ; the                 } the             } -System.out.print (i+ "\ T"); the         } the     } the }94 /* the A walk here, the time slice expires, the B runs the b Execution is complete, and the Obj object is the b thread new . the at this point, B executes, a runs again, since the time slice expires from here, after resuming the run, also continues from the current line98 execute the following code, and a thread creates an object About at this point B is created once, a is created once, the outside is at least two objects - */

[design mode] Singleton mode (consider multithreading)

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.