-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
Bitte posten Sie alle Fehler, die Sie bekommen. –
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. –
Was ist das 'n' im' CarParkControl' Konstruktor? Es gibt kein 'n'! – ArcticLord