模拟火车站卖票: 现在有窗口1, 窗口2, 窗口3同时卖一个车厢的100张火车票, 要求不能卖出重复的票,卖完就结束卖票.
package 作业;
/**
* company: www.abc.com
* Author: Administrator
* Create Data: 2019/8/13
*/
/*模拟火车站卖票:
现在有窗口1, 窗口2, 窗口3同时卖一个车厢的100张火车票,
要求不能卖出重复的票,卖完就结束卖票.
*/
public class 作业1 {
private static int count = 50;
public static void main(String[] args) {
final Object lock = new Object();
Thread t1 = new Thread() {
@Override
public void run() {
synchronized (lock) {
while (count > 0) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
count--;
System.out.println("还有" + count + "张余票__来自1号窗口");
}
}
}
};
Thread t2 = new Thread() {
@Override
public void run() {
synchronized (lock) {
while (count > 0) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
count--;
System.out.println("还有" + count + "张余票__来自2号窗口");
}
}
}
};
Thread t3 = new Thread() {
@Override
public void run() {
synchronized (lock) {
while (count > 0) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
count--;
System.out.println("还有" + count + "张余票__来自3号窗口");
}
}
}
};
t1.start();
t2.start();
t3.start();
}
}