package threadsdemo;
/**
 * ein Teil der Museums-Simulation nach Judy Bishop: die Schalter
 *
 * @author Ralph Matthes (Original: Judy Bishop)
 * @version $Revision: 1.3 $ from $Date: 1999/11/16 23:05:13 $ (+1 Stunde)
 */
class Counter extends Thread {
    /** 
     * durchschnittlicher Zeitabstand zwischen den eintreffenden
     * Besuchergruppen 
     */
    private static final int MEANARRIVALGAP=20000;
    /** 
     * ein Schalter, an dem Walkmen an Besuchergruppen ausgegeben werden
     *
     * @param m das Museumsobjekt
     * @param q die Schalternummer (von Queue)
     */
  Counter (Museum m, int q) {
    museum = m;
    queue = q;
  }
    /**
     * Decide how many walkmen are needed for a
     * a group of visitors and attempt to hire them 
     * (waiting until successful). The visitors are 
     * sent off on their own to walk around (by 
     * starting a new Visitors thread which runs
     * independently.)<br>
     * durchschnittliche Gruppengröße ist 4
     */
  public void run () {

     while (true) {
	 int w = a(7);
      museum.hire(queue, w, System.currentTimeMillis());
      new Visitors (museum, w).start();

      // Wait a bit before the next people arrive
      try {sleep(a(2*MEANARRIVALGAP));} catch(InterruptedException e) {}
    }
  }
    /** 
     * das Museumsobjekt
     */
  Museum museum;
    /**
     * die Schalternummer
     */
  int queue;

    /**
     * ergibt Zufallszahl von 1 bis zum Argument
     *
     * @param x die größtmögliche Zufallszahl
     * @return eine Zufallszahl
     */
  private static int a (int x) {
    return (int) (Math.random() * x) +1;
  }
}
