2016-04-09 3 views
-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 
+2

ご迷惑をおかけください。 –

+2

あなたはそれが動かないと思いますか?このプログラムは画面に何も印刷しないので、空白の画面が表示された場合は正常です。 –

+2

'CarParkControl'コンストラクタの' n'とは何ですか? 「n」はありません! – ArcticLord

答えて

1

は、私はあなたがPROGを得る示唆しますramをコンパイルしてから実行してください。

関連する問題