package threadsdemo;
import java.io.*;
/** 
 * ein Teil der Museums-Simulation nach Judy Bishop: das Museum
 *
 * @author Ralph Matthes (Original: Judy Bishop)
 * @version $Revision: 1.2 $ from $Date: 1999/11/16 23:05:14 $ (+1 Stunde)
 */
class Museum {
    /** 
     * das Museum, aufgefaßt als Ort der Walkmen
     *
     * @param w die Zahl der Walkmen
     */
  Museum (int w) {
    walkmen = w;
    cash = 0;
  }
    /** 
     * If there are not enough walkmen left,
     * wait until someone at another counter returns 
     * some and notifies us accordingly.
     * If the returns are not enough, we'll carry on
     * waiting.
     * Then hire out the walkmen and take the deposit.
     * Let the customers at this counter "walk away"
     * by relinquishing control of the monitor with
     * a notify call.
     *
     * @param c die Schalternummer
     * @param n die Zahl der verlangten Walkmen
     * @param start die Zeit beim Aufruf der Methode
     */
    synchronized void hire (int c,int n,long start) { 
     
    System.out.println("Counter "+(c+1)+" wants "+n);
    while (walkmen < n) {
	try { wait(); }
	catch (InterruptedException e) {}
    }
    
    walkmen -= n;
    cash += n;
    waited=System.currentTimeMillis()-start;
    System.out.println("Counter "+(c+1)+" acquires "+n+" after waiting for "
		       +waited+" milliseconds");
    System.out.println("Pool status:"+
      " Deposits "+cash+" Total "+(walkmen+cash)
      + " Walkmen "+walkmen);
    notify ();
  }
    /** 
     * Always accept replacements immediately.
     * Once the pool and deposits have been updated,
     * notify any other helper waiting for walkmen.
     *
     * @param n Anzahl der zurückgegebenen Walkmen
     */
    synchronized void replace (int n) {

    System.out.println("Replacing "+n);
    walkmen +=n;
    cash -= n;
    notify ();
  }
    /** 
     * Zahl der verfügbaren Walkmen
     */
  private static int walkmen;
    /** 
     * Geldmenge = Anzahl der ausgegebenen Walkmen
     */
  private static int cash;
    /** 
     * The waiting time in milliseconds until the walkmen have been hired out.
     */
  private static long waited; 
}
