我对这个简单的练习有一个问题:有一个停车场,一次最多允许 4 辆车,有 6 辆车。
问题是4辆车进入后,接下来的2辆车进入等待状态,从不进入。
这是我在屏幕上看到的:
Car 1 in
Car 2 in
Car 5 in
Car 6 in
Car 4 is waiting
Car 3 is waiting
Car 1 out
Car 1 in
Car 3 is waiting
Car 4 is waiting
Car 5 out
Car 5 in
Car 2 out
Car 2 in
and so on..
这里的类:
public class Car extends Thread{
int idCar;
Monitor monitor;
public Car(int id, Monitor monitor){
idCar=id;
this.monitor=monitor;
}
public void run(){
while(true){
monitor.enter(idCar);
try{
sleep(2000);
}catch(Exception e){}
monitor.exit(idCar);
}
}
}
public class Monitor {
int maxSpots;
int parkedCars;
public Monitor(int maxSpots){
this.maxSpots=maxSpots;
parkedCars=0;
}
public synchronized void enter(int idCar){
while(parkedCars==maxSpots){
try{
System.out.println("Car "+idCar+" is waiting");
wait();
}catch(Exception e){}
}
++parkedCars;
System.out.println("Car "+idCar+" in");
notifyAll();
}
public synchronized void exit(int idCar){
/*while(parkedCars==0){ // i'm not sure if this check is needed, because
try{ // no car will ever get out, if it's not in.
wait();
}catch(Exception e){}
}*/
--parkedCars;
System.out.println("Car "+idCar+" out");
notifyAll();
}
}
public class Parking {
public static void main(String[] args){
Monitor m = new Monitor(4);
Car c1 = new Car(1, m);
Car c2 = new Car(2, m);
Car c3 = new Car(3, m);
Car c4 = new Car(4, m);
Car c5 = new Car(5, m);
Car c6 = new Car(6, m);
c1.start();
c2.start();
c3.start();
c4.start();
c5.start();
c6.start();
}
}
谁能解释一下?