coderbot

Dining Philosophers #2

Jul 17th, 2014
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.90 KB | None | 0 0
  1. import java.util.concurrent.*;
  2. import java.util.*;
  3.  
  4. class Chopstick {
  5.  
  6.     private boolean taken = false;
  7.  
  8.     public synchronized
  9.             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 Philosopher implements Runnable {
  23.  
  24.     private Chopstick left;
  25.     private Chopstick right;
  26.     private final int id;
  27.     private final int ponderFactor;
  28.     private Random rand = new Random(47);
  29.  
  30.     private void pause() throws InterruptedException {
  31.         if (ponderFactor == 0) {
  32.             return;
  33.         }
  34.         TimeUnit.MILLISECONDS.sleep(
  35.                 rand.nextInt(ponderFactor * 250));
  36.     }
  37.  
  38.     public Philosopher(Chopstick left, Chopstick right,
  39.             int ident, int ponder) {
  40.         this.left = left;
  41.         this.right = right;
  42.         id = ident;
  43.         ponderFactor = ponder;
  44.     }
  45.  
  46.     public void run() {
  47.         try {
  48.             while (!Thread.interrupted()) {
  49.                 System.out.println(this + " " + "thinking");
  50.                 pause();
  51.                 // Philosopher becomes hungry
  52.                 System.out.println(this + " " + "grabbing right");
  53.                 right.take();
  54.                 System.out.println(this + " " + "grabbing left");
  55.                 left.take();
  56.                 System.out.println(this + " " + "eating");
  57.                 pause();
  58.                 right.drop();
  59.                 left.drop();
  60.             }
  61.         } catch (InterruptedException e) {
  62.             System.out.println(this + " " + "exiting via interrupt");
  63.         }
  64.     }
  65.  
  66.     public String toString() {
  67.         return "Philosopher " + id;
  68.     }
  69. }
  70.  
  71. public class FixedDiningPhilosophers {
  72.  
  73.     public static void main(String[] args) throws Exception {
  74.         int ponder = 5;
  75.         if (args.length > 0) {
  76.             ponder = Integer.parseInt(args[0]);
  77.         }
  78.         int size = 5;
  79.         if (args.length > 1) {
  80.             size = Integer.parseInt(args[1]);
  81.         }
  82.         ExecutorService exec = Executors.newCachedThreadPool();
  83.         Chopstick[] sticks = new Chopstick[size];
  84.         for (int i = 0; i < size; i++) {
  85.             sticks[i] = new Chopstick();
  86.         }
  87.         for (int i = 0; i < size; i++) {
  88.             if (i < (size - 1)) {
  89.                 exec.execute(new Philosopher(
  90.                         sticks[i], sticks[i + 1], i, ponder));
  91.             } else {
  92.                 exec.execute(new Philosopher(
  93.                         sticks[0], sticks[i], i, ponder));
  94.             }
  95.         }
  96.         if (args.length == 3 && args[2].equals("timeout")) {
  97.             TimeUnit.SECONDS.sleep(5);
  98.         } else {
  99.             System.out.println("Press ‘Enter’ to quit");
  100.             System.in.read();
  101.         }
  102.         exec.shutdownNow();
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment