public class TestTicket{
public static void main(String[] args){
SellTickets st = new SellTickets();
Thread t1 = new Thread(st,"窗口1--");
Thread t2 = new Thread(st,"窗口2--");
Thread t3 = new Thread(st,"窗口3--");
t1.start();
t2.start();
t3.start();
}
}
class SellTickets implements Runnable{
private int ticket=10;
private synchronized void SellTickets(){
try{
Thread.sleep(1000);
System.out.println(Thread.currentThread().getName() +
"第 " + ticket + "张票已经售出。" );
ticket--;
this.wait();
this.notify();
}
catch(Exception e){
e.printStackTrace();
}
}
public synchronized void run(){
while(ticket>0){
SellTickets();
}
System.out.println(Thread.currentThread().getName() + "票已经售完,正在补票中,请等待。。。");
}
}
输出结果
我想要实现3个窗口轮流售票。怎么弄 实在搞不了了