public class SellTicket implements Runnable{
static int count = 50;
static Object obj = new Object();
@Override
public void run() {
while (true){
synchronized (obj){
try {
Thread.sleep(2000);
if (count>0){
System.out.println(Thread.currentThread().getName()+"当前火车票号"+count--);
}else {
System.out.println("售卖完了");
break;
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
SellTicket st = new SellTicket();
Thread t = new Thread(st);
t.setName("第一窗口");
t.start();
SellTicket st1 = new SellTicket();
Thread t1 = new Thread(st1);
t1.setName("第二窗口");
t1.start();
SellTicket st2 = new SellTicket();
Thread t2 = new Thread(st2);
t2.setName("第三窗口");
t2.start();
}
}