1 public classSellticketImplementsRunnable {2 //Define 100 Tickets3 Private intTickets = 100;4 //to Create a Lock object5 PrivateObject obj =NewObject ();6 7 @Override8 public voidRun () {9 while(true) {Ten synchronized(obj) { one if(tickets > 0) { a Try { -Thread.Sleep (100); -}Catch(interruptedexception E) { the e.printstacktrace (); - } -System.out.println (thread.currentthread (). getName () + "selling" + (tickets--) + "ticket"); - } + } - } + } a } at - - - - - public classSellticketdemo { in public Static voidmain (string[] Args) { - //Create a Resource object toSellticket st =NewSellticket (); + - //Create three thread objects theThread T1 =NewThread (st, "window 1")); *Thread t2 =NewThread (st, "window 2")); $Thread t3 =NewThread (st, "window 3"));Panax Notoginseng - //Start Thread the T1.start (); + T2.start (); a T3.start (); the } + } - $
Window selling thread solution for tickets