Thread:
classMyThreadextendsthread{Private intticketscont=5;//There are 5 train tickets altogether . PrivateString name;//window, which is also the name of the thread PublicMyThread (String name) { This. name=name; } @Override Public voidrun () { while(ticketscont>0) {Ticketscont--;//If there's a ticket, sell a ticket.System.out.println (name+ "sold 1 tickets, the remaining votes:" +Ticketscont); } }} Public classticketsthread{ Public Static voidMain (String args[]) {//create three threads, simulate three windows to sell a ticketMyThread mt1=NewMyThread ("Windows 1"); MyThread mt2=NewMyThread ("Windows 2"); MyThread Mt3=NewMyThread ("Windows 3"); //start three threads, that is, windows, start selling ticketsMt1.start (); Mt2.start (); Mt3.start (); }}
Window 1 sold 1 tickets, the remaining votes are: 4 window 1 sold 1 tickets, the remaining votes:3 window 1 sold 1 tickets, the remaining votes are:2 window 1 sold 1 tickets, the remaining votes:1 window 1 sold 1 tickets, The remaining votes are:0 window 3 sold 1 tickets, the remaining votes:4 window 2 sold 1 tickets, the remaining votes:4 window 3 sold 1 tickets, the remaining votes are:3 window 3 sold 1 tickets, the remaining votes: 2 window 3 sold 1 tickets, the remaining votes:1 window 3 sold 1 tickets, the remaining votes are:0 window 2 sold 1 tickets, the remaining votes are:3 window 2 sold 1 tickets, the remaining votes of :2 window 2 sold 1 tickets, the remaining votes:1 window 2 sold 1 tickets, the remaining votes are:0
View Code
Runnable Interface:
classMyThread2Implementsrunnable{Private intticketscont=5;//There are 5 train tickets altogether .@Override Public voidrun () { while(true){ synchronized( This){ if(ticketscont<=0){ Break; } Ticketscont--;//If there's a ticket, sell a ticket.System.out.println (Thread.CurrentThread (). GetName ()+ "Sold 1 tickets, remaining votes:" +Ticketscont); /*try{Thread.Sleep (50); To view the effect by blocking the program} catch (Exception e) {System.out.println (e); }*/ } } }} Public classticketsrunnable{ Public Static voidMain (String args[]) {MyThread2 MT=NewMyThread2 (); //create three threads to simulate three ticketing windowsThread th1=NewThread (MT, "Window 1"); Thread Th2=NewThread (MT, "Window 2"); Thread Th3=NewThread (MT, "Window 3"); //start three threads, that is, three windows, start selling ticketsTh1.start (); Th2.start (); Th3.start (); }}
Window 1 sold 1 tickets, the remaining votes are: 4 window 1 sold 1 tickets, the remaining votes:3 window 1 sold 1 tickets, the remaining votes are:2 window 1 sold 1 tickets, the remaining votes:1 window 1 sold 1 tickets, Number of votes left:0
View Code
The thread class is a separate task for multiple threads, and runnable is a common task for multiple threads to accomplish.
We recommend that you create threads this way using runnable. The same resource in the program refers to the same Runnable object. It is necessary to join the synchronous synchronized in the secure ticket selling procedure.
Http://www.cnblogs.com/Li-Cheng/p/4332179.html
Java Multithreading thread and runnable