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