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