package com.homework;
public class BuyTickets1 implements Runnable {
static int tickets = 30;
private String name;
public BuyTickets1(String name) {
this.name = name;
}
public void run() {
for (int i = 1; i <= 35; i++) {
synchronized (this) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
tickets--;
if (tickets > 0) {
System.out.println("窗口" + name + "卖了一张票" + "还剩" + tickets
+ "张票");
} else if (tickets == 0) {
System.out.println("票已售完");
}
}
}
}
public static void main(String[] args) {
new Thread(new BuyTickets1("A")).start();
new Thread(new BuyTickets1("B")).start();
new Thread(new BuyTickets1("C")).start();
}
}
相同的票数指的哪里相同啊? 是三个窗口卖的票数相同还是剩余的票数相同啊?
synchronized线程问题,因为你每次运行并没有去计算剩余票票数,希望对你有用.....