ImportJava.text.NumberFormat;Importjava.util.ArrayList;Importjava.util.List;Importjavax.swing.text.DefaultEditorKit.InsertBreakAction; Public classTicketImplementsRunnable {Private intpoll; Private intnum = 900; Privatelist<string> list =NewArraylist<>(); Private StaticNumberFormat NF =numberformat.getinstance (); Static{nf.setminimumintegerdigits (4); Nf.setgroupingused (false); } PublicTicket () {} PublicTicket (intpoll) { This. Poll =poll; } Public voidrun () { while(true){ synchronized( This) { if(Num >=poll) {System.out.println ("The tickets are sold out."); return; } String tname=Thread.CurrentThread (). GetName (); Num++; System.out.println (Tname+ "\ t sell \ t" +Nf.format (num)); } Try{Thread.Sleep (100); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } } } Public Static voidMain (string[] args) {Runnable target=NewTicket (1000); String Windows= "ABCDE"; for(inti = 0; I<windows.length (); i++) {Thread te=NewThread (Target, "counter" +Windows.charat (i)); Te.start (); } }}
Multi-threaded Demo (Analog ticketing system)