コード例 #1
0
ファイル: Philosoph.java プロジェクト: munichsven/RMI_Server
 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();
   }
 }
コード例 #2
0
  public void run() {
    Random randomGenerator = new Random();
    try {
      while (true) {
        // Thread.sleep(randomGenerator.nextInt(100)); //not sleeping but thinking
        int leftHash = System.identityHashCode(left);
        int rightHash = System.identityHashCode(right);

        if (leftHash < rightHash) {
          System.out.println("Phil " + index + " finishes thinking.");
          left.pickup();

          System.out.println("Phil " + index + " picks up left fork.");
          right.pickup();
          System.out.println("Phil " + index + " picks up right fork.");
          Thread.sleep(randomGenerator.nextInt(100)); // eating
          System.out.println("Phil " + index + " finishes eating.");
          left.putdown();
          System.out.println("Phil " + index + " puts down left fork.");
          right.putdown();
          System.out.println("Phil " + index + " puts down right fork.");
        } else {
          System.out.println("Phil " + index + " finishes thinking.");
          right.pickup();

          System.out.println("Phil " + index + " picks up left fork.");
          left.pickup();
          System.out.println("Phil " + index + " picks up right fork.");
          Thread.sleep(randomGenerator.nextInt(100)); // eating
          System.out.println("Phil " + index + " finishes eating.");
          right.putdown();
          System.out.println("Phil " + index + " puts down left fork.");
          left.putdown();
          System.out.println("Phil " + index + " puts down right fork.");
        }
      }
    } catch (InterruptedException e) {
      System.out.println("Don't disturb me while I am sleeping, well, thinking.");
    }
  }
コード例 #3
0
    public Node(Fork parent, int id) {
      this.parent = parent;
      this.id = id;

      if (parent != null) parent.setChild(id, this);
    }
コード例 #4
0
 public Fork promote(Program.Branch<S> branch) {
   Fork f = new Fork(branch, parent, id);
   f.setAggregateCount(aggregateCount);
   f.setAggregateSum(aggregateSum);
   return f;
 }
コード例 #5
0
ファイル: Philosoph.java プロジェクト: munichsven/RMI_Server
  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;
  }