public static void finalize(Philosopher[] phils) { long totalWait = 0; long minWait = Long.MAX_VALUE; long maxWait = 0; int minEat = Integer.MAX_VALUE; int maxEat = 0; int sumEat = 0; for (Philosopher phil : phils) { System.out.println( "[Philosopher " + phil.getPosition() + "] ate " + phil.eatCount + " times and waited " + phil.waitTime + " ms"); totalWait += phil.waitTime; sumEat += phil.eatCount; minEat = Math.min(phil.eatCount, minEat); maxEat = Math.max(phil.eatCount, maxEat); minWait = Math.min(phil.waitTime, minWait); maxWait = Math.max(phil.waitTime, maxWait); } System.out.println( "Avg: waited " + totalWait / count + " ms, ate " + sumEat / count + " times."); System.out.println("Min: waited " + minWait + " ms, ate " + minEat + " times."); System.out.println("Max: waited " + maxWait + " ms, ate " + maxEat + " times."); }
public static void main(String[] args) { DiningTable diningTable = new DiningTable(); List<Thread> philosophers = new ArrayList<>(); for (int i = 0; i < 5; i++) { Philosopher philosopher = new Philosopher("Philosopher n°" + i); diningTable.addPhilosopher(philosopher); philosophers.add(new Thread(philosopher, philosopher.getName())); } for (Thread philosopher : philosophers) { philosopher.start(); } }