This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

[TIJ4] Dining Philisophers problem

By: a guest on Dec 8th, 2012  |  syntax: Java  |  size: 2.51 KB  |  views: 40  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
This paste has a previous version, view the difference. Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. //: concurrency/DeadlockingDiningPhilosophers.java
  2. package concurrency;
  3.  
  4. import java.util.concurrent.*;
  5.  
  6. public class DeadlockingDiningPhilosophers {
  7.   public static void main(String[] args) throws Exception {
  8.     int ponder = 5;
  9.     if(args.length > 0)
  10.       ponder = Integer.parseInt(args[0]);
  11.     int size = 5;
  12.     if(args.length > 1)
  13.       size = Integer.parseInt(args[1]);
  14.     ExecutorService exec = Executors.newCachedThreadPool();
  15.     Chopstick[] sticks = new Chopstick[size];
  16.     for(int i = 0; i < size; i++)
  17.       sticks[i] = new Chopstick();
  18.     for(int i = 0; i < size; i++)
  19.       exec.execute(new Philosopher(
  20.         sticks[i], sticks[(i+1) % size], i, ponder));
  21.     if(args.length == 3 && args[2].equals("timeout"))
  22.       TimeUnit.SECONDS.sleep(5);
  23.     else {
  24.       System.out.println("Press 'Enter' to quit");
  25.       System.in.read();
  26.     }
  27.     exec.shutdownNow();
  28.   }
  29. }
  30.  
  31. //: concurrency/Philosopher.java
  32.                                                                      
  33. package concurrency;
  34.  
  35. import java.util.concurrent.*;
  36. import java.util.*;
  37. import static util.Print.*;
  38.  
  39. public class Philosopher implements Runnable {
  40.   private Chopstick left;
  41.   private Chopstick right;
  42.   private final int id;
  43.   private final int ponderFactor;
  44.   private Random rand = new Random(47);
  45.   private void pause() throws InterruptedException {
  46.     if(ponderFactor == 0) return;
  47.     TimeUnit.MILLISECONDS.sleep(
  48.       rand.nextInt(ponderFactor * 250));
  49.   }
  50.   public Philosopher(Chopstick left, Chopstick right,
  51.     int ident, int ponder) {
  52.     this.left = left;
  53.     this.right = right;
  54.     id = ident;
  55.     ponderFactor = ponder;
  56.   }
  57.   public void run() {
  58.     try {
  59.       while(!Thread.interrupted()) {
  60.         print(this + " " + "thinking");
  61.         pause();                                                  
  62.         print(this + " " + "grabbing right");
  63.         right.take();
  64.         print(this + " " + "grabbing left");
  65.         left.take();
  66.         print(this + " " + "eating");
  67.         pause();
  68.         right.drop();
  69.         left.drop();
  70.       }
  71.     } catch(InterruptedException e) {
  72.       print(this + " " + "exiting via interrupt");
  73.     }
  74.   }
  75.   public String toString() { return "Philosopher " + id; }
  76. } ///:~
  77.  
  78. //: concurrency/Chopstick.java
  79.  
  80. package concurrency;
  81.  
  82. public class Chopstick {
  83.   private boolean taken = false;
  84.   public synchronized
  85.   void take() throws InterruptedException {
  86.     while(taken)
  87.       wait();
  88.     taken = true;
  89.   }
  90.   public synchronized void drop() {
  91.     taken = false;
  92.     notifyAll();
  93.   }
  94. } ///:~
clone this paste RAW Paste Data