模拟售票窗口,用4个线程模拟4个窗口在售票,共有8张票,用线程同步来实现

//模拟售票窗口,用4个线程模拟4个窗口在售票,共有8张票,用线程同步来实现
public class Ticket {

public static void main(String[] args) {
for(int i=0;i<4;i++){
new SellWindow().start();
}
}
}

class SellWindow extends Thread {
private static int ticketAll = 100;
private static Object obj = new Object();

public void run() {
while (true) {
synchronized (obj) {
if (ticketAll > 0) {
/* try {
Thread.sleep(10);
}
catch (Exception ex) {
}*/

ticketAll--;
//System.out.println(getName() + "售票" + ticketAll);
System.out.println (getName() + " sell " + "Tickets_" + (100-ticketAll));
} else {
break;
}
}
}
}

}

posted @ 2018-03-16 12:26  一纸年华  阅读(399)  评论(0编辑  收藏  举报