coderbot

Dining Philosophers #3

Jul 17th, 2014
309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.26 KB | None | 0 0
  1. import java.util.concurrent.*;
  2. import java.util.*;
  3. import static net.mindview.util.Print.*;
  4.  
  5. class Chopstick {
  6.  
  7.     private boolean taken = false;
  8.  
  9.     public synchronized void take() throws InterruptedException {
  10.         while (taken) {
  11.             wait();
  12.         }
  13.         taken = true;
  14.     }
  15.  
  16.     public synchronized void drop() {
  17.         taken = false;
  18.         notifyAll();
  19.     }
  20. }
  21.  
  22. class Bin {
  23.  
  24.     BlockingQueue<Chopstick> bin = new LinkedBlockingQueue<>();
  25.  
  26.     public void put(Chopstick stick) throws InterruptedException {
  27.         bin.put(stick);
  28.     }
  29.  
  30.     public Chopstick get() throws InterruptedException {
  31.         return bin.take();
  32.     }
  33. }
  34.  
  35. class Philosopher implements Runnable {
  36.  
  37.     private Chopstick left;
  38.     private Chopstick right;
  39.     private LinkedBlockingQueue<Chopstick> bin;
  40.     private final int id;
  41.     private final int ponderFactor;
  42.     private Random rand = new Random(47);
  43.  
  44.     private void pause() throws InterruptedException {
  45.         if (ponderFactor == 0) {
  46.             return;
  47.         }
  48.         TimeUnit.MILLISECONDS.sleep(rand.nextInt(ponderFactor * 250));
  49.     }
  50.  
  51.     public Philosopher(Chopstick left, Chopstick right,
  52.             LinkedBlockingQueue<Chopstick> bin, int ident, int ponder) {
  53.         this.left = left;
  54.         this.right = right;
  55.         this.bin = bin;
  56.         id = ident;
  57.         ponderFactor = ponder;
  58.     }
  59.  
  60.     public void run() {
  61.         try {
  62.             while (!Thread.interrupted()) {
  63.                 print(this + " " + "thinking");
  64.                 pause();
  65.                 // Philosopher becomes hungry
  66.                 print(this + " taking first, right chopstick");
  67.                 right = bin.take();
  68.                 print(this + " taking second, left chopstick");
  69.                 left = bin.take();
  70.                 print(this + " eating");
  71.                 pause();
  72.                 print(this + " returning chopsticks");
  73.                 bin.put(right);
  74.                 bin.put(left);
  75.             }
  76.         } catch (InterruptedException e) {
  77.             print(this + " " + "exiting via interrupt");
  78.         }
  79.     }
  80.  
  81.     public String toString() {
  82.         return "Philosopher " + id;
  83.     }
  84. }
  85.  
  86. public class DeadlockingDiningPhilosophers {
  87.  
  88.     public static void main(String[] args) throws Exception {
  89.         int ponder = 0;
  90.         if (args.length > 0) {
  91.             ponder = Integer.parseInt(args[0]);
  92.         }
  93.         int size = 5;
  94.         if (args.length > 1) {
  95.             size = Integer.parseInt(args[1]);
  96.         }
  97.         ExecutorService exec = Executors.newCachedThreadPool();
  98.         // chopstick bin:
  99.         LinkedBlockingQueue<Chopstick> bin = new LinkedBlockingQueue<>();
  100.         Chopstick[] sticks = new Chopstick[size];
  101.         for (int i = 0; i < size; i++) {
  102.             sticks[i] = new Chopstick();
  103.             bin.put(sticks[i]);
  104.         }
  105.         for (int i = 0; i < size; i++) {
  106.             exec.execute(new Philosopher(sticks[i], sticks[(i + 1) % size], bin, i, ponder));
  107.         }
  108.         if (args.length == 3 && args[2].equals("timeout")) {
  109.             TimeUnit.SECONDS.sleep(5);
  110.         } else {
  111.             System.out.println("Press 'Enter' to quit");
  112.             System.in.read();
  113.         }
  114.         exec.shutdownNow();
  115.     }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment