//: concurrency/DeadlockingDiningPhilosophers.java package concurrency; import java.util.concurrent.*; public class DeadlockingDiningPhilosophers { public static void main(String[] args) throws Exception { int ponder = 5; if(args.length > 0) ponder = Integer.parseInt(args[0]); int size = 5; if(args.length > 1) size = Integer.parseInt(args[1]); ExecutorService exec = Executors.newCachedThreadPool(); Chopstick[] sticks = new Chopstick[size]; LinkedBlockingQueue bin = new LinkedBlockingQueue(); for(int i = 0; i < size; i++) sticks[i] = new Chopstick(); for(int i = 0; i < size; i++) exec.execute(new Philosopher( sticks[i], sticks[(i+1) % size], bin, i, ponder)); if(args.length == 3 && args[2].equals("timeout")) TimeUnit.SECONDS.sleep(5); else { System.out.println("Press 'Enter' to quit"); System.in.read(); } exec.shutdownNow(); } } //: concurrency/Philosopher.java // A dining philosopher package concurrency; import java.util.concurrent.*; import java.util.*; import static util.Print.*; public class Philosopher implements Runnable { private BlockingQueue bin; private Chopstick left; private Chopstick right; private final int id; private final int ponderFactor; private Random rand = new Random(47); private void pause() throws InterruptedException { if(ponderFactor == 0) return; TimeUnit.MILLISECONDS.sleep( rand.nextInt(ponderFactor * 250)); } public Philosopher(Chopstick left, Chopstick right, BlockingQueue bin, int ident, int ponder) { this.left = left; this.right = right; this.bin = bin; id = ident; ponderFactor = ponder; } public void run() { try { print(this + " " + "thinking"); pause(); // Philosopher becomes hungry print(this + " " + "grabbing right"); right.take(); print(this + " " + "grabbing left"); left.take(); print(this + " " + "eating"); pause(); right.drop(); left.drop(); print("putting left to bin."); bin.put(left); while(!Thread.interrupted()) { print(this + " " + "thinking"); pause(); // Philosopher becomes hungry print(this + " " + "grabbing right from bin"); right = bin.take(); print(this + " " + "grabbing left from bin"); left = bin.take(); print(this + " " + "eating"); pause(); print(this + " " + "putting right to bin "); bin.put(right); print(this + " " + "putting left to bin "); bin.put(left); } } catch(InterruptedException e) { print(this + " " + "exiting via interrupt"); } } public String toString() { return "Philosopher " + id; } } ///:~ //: concurrency/Chopstick.java package concurrency; public class Chopstick { private boolean taken = false; public synchronized void take() throws InterruptedException { while(taken) wait(); taken = true; } public synchronized void drop() { taken = false; notifyAll(); } } ///:~