Java threads as much as selling a ticket window problem (Runnable)



/ **
* Example: Create three window selling tickets, the total number of votes for the 100 that implement Runnable interface mode
* thread security problems, to be resolved.
* /
Class the Window1 the implements the Runnable {

Private int Ticket = 100;

@Override
public void RUN () {
the while (to true) {
IF (Ticket> 0) {
. System.out.println (Thread.currentThread () getName () + " : selling a ticket, the ticket number is: "ticket +);
ticket--;
} the else {
BREAK;
}
}
}
}


public class WindowTest1 {
public static void main (String [] args) {
the Window1 the Window1 new new = W ();

the Thread t1 = new Thread (w);
Thread t2 = new Thread(w);
Thread t3 = new Thread(w);

t1.setName("窗口1");
t2.setName("窗口2");
t3.setName("窗口3");

t1.start();
t2.start();
t3.start();
}

}

Guess you like

Origin www.cnblogs.com/wpy188/p/12094095.html