Multithreading (mock tickets)-----Java Basics Summary

Source: Internet
Author: User

This time the problem introduced more in-depth, if read this blog, do not read the next article, you will be very confused force. Code:
1  PackageCom.day13.math;2 /** 3 * Class Description: Simulate three windows simultaneous ticketing4 * @authorAuthor: Chenyanlong5 * @versionCreation Date: October 29, 20176 */7  Public classThreadTest1 {8 9      Public Static voidMain (string[] args) {Ten         //Create three thread objects OneSaleticket saleticket1=NewSaleticket ("Windows 1"); ASaleticket saleticket2=NewSaleticket ("Windows 2"); -Saleticket saleticket3=NewSaleticket ("Windows 3"); -          the         //Start Ticketing - Saleticket1.start (); - Saleticket2.start (); - Saleticket3.start (); +     } - } +  A //the window of the ticket at classSaleticketextendsthread{ -      -     intnum=50;//Number of votes -      -      PublicSaleticket (String name) { -         Super(name); in     } -      to  +  - @Override the      Public voidrun () { *          while(true){ $             if(num>0){Panax NotoginsengSystem.out.println (Thread.CurrentThread (). GetName () + "sold the first" +num+ "Ticket"); -num--; the}Else{ +System.out.println ("Tickets are sold out!") "); A                  Break; the             } +         } -     } $}
View CodeOperating effect:

Reasons for the present:

Workaround: Modify this line of code is OK
1 Static int num=50; // Number of votes
View CodeRe-run Effect:

Once again the analysis, suddenly don't want to write, ah, write it out

Workaround:

Forget it, copy the source directly.
1  PackageCom.day13.math;2 /** 3 * Class Description: Simulate three windows simultaneous ticketing4 * @authorAuthor: Chenyanlong5 * @versionCreation Date: October 29, 20176 */7  Public classThreadTest1 {8 9      Public Static voidMain (string[] args) {Ten         //Create three thread objects OneSaleticket saleticket1=NewSaleticket ("Windows 1"); ASaleticket saleticket2=NewSaleticket ("Windows 2"); -Saleticket saleticket3=NewSaleticket ("Windows 3"); -          the         //Start Ticketing - Saleticket1.start (); - Saleticket2.start (); - Saleticket3.start (); +     } - } +  A //the window of the ticket at classSaleticketextendsthread{ -      -     Static intnum=50;//Number of votes -     StaticObject o=NewObject (); -      PublicSaleticket (String name) { -         Super(name); in     } -      to  + @Override -      Public voidrun () { the          while(true){ *             //Synchronizing code blocks: $             synchronized(o) {Panax Notoginseng                 if(num>0){ -System.out.println (Thread.CurrentThread (). GetName () + "sold the first" +num+ "Ticket"); thenum--; +}Else{ ASystem.out.println ("Tickets are sold out!") "); the                      Break; +                 } -             } $         } $     } -}
View Code

Synchronous code block principle:

Operating effect:

Optimize the code again:
1  PackageCom.day13.math;2 /** 3 * Class Description: Simulate three windows simultaneous ticketing4 * @authorAuthor: Chenyanlong5 * @versionCreation Date: October 29, 20176 */7  Public classThreadTest1 {8 9      Public Static voidMain (string[] args) {Ten         //Create three thread objects OneSaleticket saleticket1=NewSaleticket ("Windows 1"); ASaleticket saleticket2=NewSaleticket ("Windows 2"); -Saleticket saleticket3=NewSaleticket ("Windows 3"); -          the         //Start Ticketing - Saleticket1.start (); - Saleticket2.start (); - Saleticket3.start (); +     } - } +  A //the window of the ticket at classSaleticketextendsthread{ -      -     Static intnum=50;//Number of votes -     StaticObject o=NewObject (); -      PublicSaleticket (String name) { -         Super(name); in     } -      to  + @Override -      Public voidrun () { the          while(true){ *             //Synchronizing code blocks: $             synchronized(o) {Panax Notoginseng                 if(num>0){ -System.out.println (Thread.CurrentThread (). GetName () + "sold the first" +num+ "Ticket"); thenum--; +                     Try { AThread.Sleep (100);//each thread executes once and sleeps 100 milliseconds the}Catch(interruptedexception e) { +                         //TODO auto-generated Catch block - e.printstacktrace (); $                     } $}Else{ -System.out.println ("Tickets are sold out!") "); -                      Break; the                 } -             }Wuyi         } the     } -}
View CodeOperating effect:

Multithreading (mock tickets)-----Java Basics Summary

Related Article

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.