Beispiel #1
0
  public void run() {

    final Alternative alt = new Alternative(new Guard[] {in, out});
    final int IN = 0, OUT = 1;

    final Random rand = new Random();
    final CSTimer tim = new CSTimer();

    int a = -1, b = -1;

    while (true) {
      int period = (rand.nextInt()) % 16;
      tim.sleep(period);
      a++;
      switch (alt.fairSelect()) {
        case IN:
          b = in.read();
          System.out.println(b);
          break;
        case OUT:
          out.write(a);
          System.out.println("\t\t" + a);
          break;
      }
    }
  }
Beispiel #2
0
  public void run() {
    while (true) {

      // If arrival event is triggered
      if (arrival_event.pending()) {
        arrival_event.read();

        arrive.write(car_number);
        int spaces = arrive_response.read();
        // Remove check for Lock free (queue, [buffer])
        if (spaces != -1) {
          System.out.println("Car arrive"); // + car_number + " Arrive. Spaces left: " + spaces);
        } else {
          System.out.println("No space in carpark");
        }
        car_number++;
      }
    }
  }