/*
* 本程序目的:解决卖重复票 还有 负数票的情况
*
*
* */
public class SynchronizedTest1 implements Runnable {
private int tickets = -1;
@Override
public void run() {
for (int i = 0; i < 50; i++) {
synchronized (this) {
if (tickets > 0) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()
+ "号窗口卖出" + this.tickets-- + "号票");
}
}
}
}
public static void main(String[] args) {
SynchronizedTest test = new SynchronizedTest();
Thread t1 = new Thread(test);
Thread t2 = new Thread(test);
Thread t3 = new Thread(test);
Thread t4 = new Thread(test);
t1.start();
t2.start();
t3.start();
t4.start();
}