加上static, synchronized 关键字呢??
如果用static 修饰车票的数量,然后在在车票减少的地方加上synchronized可以吗?
public class Ticket extends Thread {
private static int tickets = 5;
public void run() {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
while (tickets > 0) {
synchronized (this) {
tickets--;
System.out.println(Thread.currentThread().getName() + " has " + tickets + " tickets left!");
}
Thread.yield();
}
}
}
个人亲测不行,但是不知道问题在哪?敬求指教!!!