-3
私はプロデューサー/コンシューマーについて学ぶのを助けるためにこの例を学校で与えられましたが、私はそれを理解できません。私はこれで一日中過ごし、どこにもいない。私はこのプロデューサー/コンシューマーの例を理解していません
誰も私になぜそれが動かないのか教えてもらえますか?この行は何のn
がないので、それは
でなければなりません
capacity = spaces = n;
をコンパイルしません
おかげ
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
ご迷惑をおかけください。 –
あなたはそれが動かないと思いますか?このプログラムは画面に何も印刷しないので、空白の画面が表示された場合は正常です。 –
'CarParkControl'コンストラクタの' n'とは何ですか? 「n」はありません! – ArcticLord