我用java模拟了一个汽车行驶的程序,想实现以下功能。
如果堵车了,Car线程wait,直到不堵车Car线程notify
if(!bss.isOutCarFull()){//若BSS路上的车未饱和
this.notify();//唤醒当前线程
bss.OutCar++;
Thread.sleep(20000);//车辆运行20秒
System.out.println(name+"已到达BSS,移出系统。");
this.destroyCar();//销毁车辆
bss.OutCar--;
}else{//若BSS路上的车已经饱和
this.wait();//阻塞当前线程
}
不知道能不能这样用,大家帮忙给看看吧
------解决方案--------------------
synchronized (obj) {
while (<condition does not hold>)
obj.wait(timeout);
... // Perform action appropriate to condition
}