Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

[TIJ4] [diff] Dining Philisophers problem

By: a guest on Dec 8th, 2012  |  syntax: Diff  |  size: 3.00 KB  |  views: 43  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. === modified file 'concurrency/DeadlockingDiningPhilosophers.java'
  2. --- concurrency/DeadlockingDiningPhilosophers.java      2012-12-04 05:49:42 +0000
  3. +++ concurrency/DeadlockingDiningPhilosophers.java      2012-12-08 13:48:47 +0000
  4. @@ -15,11 +15,12 @@
  5.        size = Integer.parseInt(args[1]);
  6.      ExecutorService exec = Executors.newCachedThreadPool();
  7.      Chopstick[] sticks = new Chopstick[size];
  8. +       LinkedBlockingQueue<Chopstick> bin = new LinkedBlockingQueue<Chopstick>();
  9.      for(int i = 0; i < size; i++)
  10.        sticks[i] = new Chopstick();
  11.      for(int i = 0; i < size; i++)
  12. -      exec.execute(new Philosopher(
  13. -        sticks[i], sticks[(i+1) % size], i, ponder));
  14. +               exec.execute(new Philosopher(
  15. +                 sticks[i], sticks[(i+1) % size], bin, i, ponder));
  16.      if(args.length == 3 && args[2].equals("timeout"))
  17.        TimeUnit.SECONDS.sleep(5);
  18.      else {
  19. @@ -29,3 +30,12 @@
  20.      exec.shutdownNow();
  21.    }
  22.  } /* (Execute to see output) *///:~
  23. +
  24. +/**
  25. + * Ex21(page1229): When the Philosopher puts only one chopstick in the
  26. + * bin, then the dealock does occur. If the Philosopher puts two
  27. + * chopsticks the bin -- we end having 10 chopsticks in the queue --
  28. + * every chopstick is shared by two Philosopher, so in this case
  29. + * (though it is erroneus, we don't reach Deadlock.)
  30. + */
  31. +
  32.  
  33. === modified file 'concurrency/Philosopher.java'
  34. --- concurrency/Philosopher.java        2012-12-04 05:49:42 +0000
  35. +++ concurrency/Philosopher.java        2012-12-08 13:48:47 +0000
  36. @@ -7,6 +7,7 @@
  37.  import static util.Print.*;
  38.  
  39.  public class Philosopher implements Runnable {
  40. +       private BlockingQueue<Chopstick> bin;
  41.    private Chopstick left;
  42.    private Chopstick right;
  43.    private final int id;
  44. @@ -17,16 +18,16 @@
  45.      TimeUnit.MILLISECONDS.sleep(
  46.        rand.nextInt(ponderFactor * 250));
  47.    }
  48. -  public Philosopher(Chopstick left, Chopstick right,
  49. +       public Philosopher(Chopstick left, Chopstick right, BlockingQueue<Chopstick> bin,
  50.      int ident, int ponder) {
  51.      this.left = left;
  52.      this.right = right;
  53. +       this.bin = bin;
  54.      id = ident;
  55.      ponderFactor = ponder;
  56.    }
  57.    public void run() {
  58.      try {
  59. -      while(!Thread.interrupted()) {
  60.          print(this + " " + "thinking");
  61.          pause();
  62.          // Philosopher becomes hungry
  63. @@ -36,9 +37,22 @@
  64.          left.take();
  65.          print(this + " " + "eating");
  66.          pause();
  67. -        right.drop();
  68. +        right.drop();
  69.          left.drop();
  70. -      }
  71. +               print("putting left to bin."); bin.put(left);
  72. +               while(!Thread.interrupted()) {
  73. +                       print(this + " " + "thinking");
  74. +                       pause();
  75. +                       // Philosopher becomes hungry
  76. +                       print(this + " " + "grabbing right from bin");
  77. +                       right = bin.take();
  78. +                       print(this + " " + "grabbing left from bin");
  79. +                       left = bin.take();
  80. +                       print(this + " " + "eating");
  81. +                       pause();
  82. +                       print(this + " " + "putting right to bin "); bin.put(right);
  83. +                       print(this + " " + "putting left to bin "); bin.put(left);
  84. +               }
  85.      } catch(InterruptedException e) {
  86.        print(this + " " + "exiting via interrupt");
  87.      }