Esempio n. 1
0
 private void releaseForks(Seat crntSeat) {
   Fork left = crntSeat.getLeft();
   left.getSemaphore().release();
   if (crntSeat.equals(seatList.getLast()) && this.client.hasNeighborClient()) {
     System.out.println("Philosoph " + this.getPhilosophsId() + " gibt Nachbargabel zurueck!");
     this.client.callNeighborToReleaseFork();
   } else {
     crntSeat.getRight().getSemaphore().release();
   }
 }
Esempio n. 2
0
  private boolean[] getForks(final Seat seat) {
    Fork left = seat.getLeft();
    boolean hasRight = false;
    boolean hasBoth = false;
    boolean exception = false; // fuer Abbruchbedingung, falls anfordern der
    // Gabel scheitert
    boolean[] hasBoth_exception = new boolean[2];

    while (!hasBoth && !exception) {
      int tries = 0;
      try {
        left.getSemaphore().acquire();
      } catch (InterruptedException e1) {
        System.out.println("This shouldnt happen.");
        e1.printStackTrace();
      }

      while (!hasRight && tries <= Constant.TRIES_TO_GET_FORK) {
        if (seat.equals(seatList.getLast())) {
          // Unterscheidung ob rechte Gabel lokal oder remote liegt
          if (this.client.hasNeighborClient()) {
            hasRight = this.client.callNeighborToBlockFork();
          } else {
            try {
              hasRight =
                  seat.getRight()
                      .getSemaphore()
                      .tryAcquire(Constant.TIME_UNTIL_NEW_FORKTRY, TimeUnit.MILLISECONDS);
            } catch (InterruptedException | NullPointerException e) {
              System.out.println("Fehler beim Anfordern der rechten Gabel!");
              e.printStackTrace();
              exception = true;
            }
          }
        } else {
          try {
            hasRight =
                seat.getRight()
                    .getSemaphore()
                    .tryAcquire(Constant.TIME_UNTIL_NEW_FORKTRY, TimeUnit.MILLISECONDS);
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
        }

        tries++;
      }
      hasBoth = hasRight;
      if (!hasBoth) {
        left.getSemaphore().release();
        try {
          Thread.sleep(Constant.TIME_UNTIL_NEW_FORKTRY);
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      }
    }
    hasBoth_exception[0] = hasBoth;
    hasBoth_exception[1] = exception;
    return hasBoth_exception;
  }