Ticket 2-extends Thread

Source: Internet
Author: User

1  PackageXiancheng;2 3  Public classTest4extendsthread{4     Static intpiaos=10;5      Public voidRun ()6     {7         //Sell Tickets8 //While (true)9 //        {Ten //try { One //Thread.Sleep (+); A //} catch (Interruptedexception e) { - //                //TODO Auto-generated catch block - //e.printstacktrace (); the //            } - //             - //            //System.out.println (Thread.CurrentThread (). GetName () + "number of votes left:" +piaos); - //piaos--; + //if (piaos>=0) - //            { + //System.out.println (This.getname () + "number of votes left:" +piaos); A //                 at //            } - //Else - //            { - //System.out.println ("End of Ticket"); - //Break ; - //            } in //        } - //         to          +          while(true) -         { the             Try { *Thread.Sleep (1000); $             } Panax Notoginseng             Catch(interruptedexception e) { -                 //TODO Auto-generated catch block the e.printstacktrace (); +             } A              the             //System.out.println (Thread.CurrentThread (). GetName () + "number of votes left:" +piaos); +             synchronized("") -             { $piaos--; $             if(piaos>=0) -             { -System.out.println ( This. GetName () + "number of votes left:" +Piaos); the                  -             }Wuyi             Else the             { -System.out.println ("End of Ticket"); Wu                  Break; -             } About         } $         } -          -     } -  A      Public Static voidMain (string[] args) { +Test4 p4=NewTest4 (); theP4.setname ("Ticket window One:"); - P4.start (); $          theTest4 p4_1=NewTest4 (); theP4_1.setname ("Ticket Window II:"); the P4_1.start (); the          -Test4 p4_2=NewTest4 (); inP4_2.setname ("Ticket window Three:"); the P4_2.start (); the  About     } the  the}

Ticket 2-extends Thread

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.