2016-04-09 9 views
-3

Ich wurde dieses Beispiel in der Schule gegeben, um mir zu helfen, über Erzeuger/Verbraucher zu lernen, und ich kann keinen Sinn davon machen. Ich habe den ganzen Tag damit verbracht und komme nicht weiter.Ich verstehe diesen Produzenten/Verbraucherbeispiel nicht

Kann mir jemand sagen, warum es nicht läuft?

Dank

public class CarPark { 
    public static void main(String[] args) { 

     CarParkControl carpark = new CarParkControl(4); 

     Thread arrivals = new Thread(new Arrivals(carpark)); 

     Thread departures = new Thread(new Departures(carpark)); 

     arrivals.start(); 
     departures.start(); 

    }//main 
}//CarPark 



class Arrivals implements Runnable { 

    CarParkControl carpark; 
    Arrivals(CarParkControl c) {carpark = c;} 

    public void run() { 
     try { 
     while(true) { 
      carpark.arrive(); 
      Time.delay(RandomGenerator.integer(0,520)); 
     } 
     } catch (InterruptedException e){} 
    } 
} 



class Departures implements Runnable { 

    CarParkControl carpark; 
    Departures(CarParkControl c) {carpark = c;} 

    public void run() { 
     try { 
     while(true) { 
      carpark.depart(); 
      Time.delay(RandomGenerator.integer(0,520)); 
     } 
     } catch (InterruptedException e){} 
    } 
} 



class CarParkControl { 

    protected int spaces; 
    protected int capacity; 

    CarParkControl(int capacity) 
    {capacity = spaces = n;} 

    synchronized void arrive() throws InterruptedException { 
     while (spaces==0) wait(); 
     --spaces; 
     notify(); 
    }//arrive 

    synchronized void depart() throws InterruptedException { 
     while (spaces==capacity) wait(); 
     ++spaces; 
     notify(); 
    }//depart 

}//CarParkControl 
+2

Bitte posten Sie alle Fehler, die Sie bekommen. –

+2

Was lässt Sie denken, dass es nicht läuft? Dieses Programm druckt nie etwas auf den Bildschirm, wenn Sie also einen leeren Bildschirm haben, ist das normal. –

+2

Was ist das 'n' im' CarParkControl' Konstruktor? Es gibt kein 'n'! – ArcticLord

Antwort

1

Diese Linie nicht

capacity = spaces = n; 

es sein sollte

this.capacity = spaces = capacity; 

nicht kompiliert, da es keine n

ist empfehle ich Ihnen das Prog erhalten rammen Sie zum Kompilieren, bevor Sie versuchen, es auszuführen.