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(); } }
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."); } }
public Node(Fork parent, int id) { this.parent = parent; this.id = id; if (parent != null) parent.setChild(id, this); }
public Fork promote(Program.Branch<S> branch) { Fork f = new Fork(branch, parent, id); f.setAggregateCount(aggregateCount); f.setAggregateSum(aggregateSum); return f; }
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; }