模拟售票窗口,用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;
}
}
}
}

}

原文地址:https://www.cnblogs.com/nullcodeworld/p/8580380.html