- Simultaneous ticketing at the train station ticketing window
Public classXianchenglianxi { Public Static voidMain (String arg[]) {LongBegin =System.currenttimemillis (); MyThread myth_1=NewMyThread ("1");//To create a thread objectMyThread myth_2 =NewMyThread ("2"); MyThread Myth_3=NewMyThread ("3"); MyThread Myth_4=NewMyThread ("4"); MyThread myth_5=NewMyThread ("5"); Myth_1.setpriority (5);//Set Thread Priority 1 minimum 10 maximumMyth_2.setpriority (6); Myth_3.setpriority (10); Myth_4.setpriority (1); Myth_5.setpriority (8); Myth_1.start (); //Start ThreadMyth_2.start (); Myth_3.start (); Myth_4.start (); Myth_5.start (); }}classMyThreadextendsThread {//inherits the thread class in order to call its Run methodString name; intTicket = 5;//Total votes PublicMyThread (String name) { This. Name =name; } Public voidRun () {//method Overrides for(inti = Ticket;i >= 0;i--){ if(I > 0) {System.out.println ("Window:" +name+ "ticket success" + "\ T" + "excess ticket" +i); } Else{System.out.println ("Window:" +name+ "ticket success \ t" +i+ "\ t window close"); } } }}
Multi-threaded practice (simple simulation of railway station multi-window simultaneous ticketing)