public class TicketThread implements Runnable {
static int tickets = 100;// 火车票数量
Object obj = new Object();
@Override
public void run() {
// 出售火车票
while (true) {
/*
* synchronized (obj) { method(); }
*/
method();
// method2();
}
}
private synchronized void method() {
if (tickets > 0) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + ":" + tickets--);
}
}
请问这段代码中的if (tickets > 0) 能否改成if (tickets<= 0){break;} 改完后打印的有负数 ,是什么问题呢?
言曌博客liuyanzhao_com
相关分类