title: Dining Philosopher Java
body: <p>This is my code: </p>
  
  <p>Table.java</p>
  
  <pre><code>public class Table {
      Lock mutex; 
      int nbrOfChopsticks;
      private boolean chopstick[]; //true if chopstick[i] is available
  
      public Table(int nbrOfSticks){
          nbrOfChopsticks=nbrOfSticks;
          chopstick=new boolean[nbrOfChopsticks];
          for(int i=0;i&lt;nbrOfChopsticks;i++){
              chopstick[i]=true;
          }
      }
  
  public synchronized void getLeft(int n) throws InterruptedException{
      //philosopher n picks up its left chopstick
      chopstick[n] = false; 
  }
  
  public synchronized void getRight(int n) throws InterruptedException{
      //philosopher n picks up its right chopstick
     chopstick[n] = false; 
  }
  
  public synchronized void releaseLeft(int n){
      //philosopher n puts down its left chopstick
      int pos = n + 1;
      if (pos == nbrOfChopsticks) {
          pos = 0;
      }
      chopstick[pos] = true;
  }
  
  public synchronized void releaseRight(int n){
      //philosopher n puts down its right chopstick
      int pos = n + 1;
      if (pos == nbrOfChopsticks) {
          pos = 0;
      }
      chopstick[pos] = true;
  }
  
  
  }
  </code></pre>
  
  <p>Philisopher.java</p>
  
  <pre><code>   public class Philosopher implements Runnable{
        private int id;
        private Table myTable;
  
        public Philosopher(int pid,Table tab){
            id=pid;
            myTable=tab;
        }
  
        @Override
        public void run(){
             for(int i=0;i&lt;100;i++){
                 try {
                   //think
                   System.out.println("Philosopher " + id + " thinks. Iteration "+ i);
                   Thread.sleep((int)(Math.random()*100));
                   //pick up chopsticks
                   System.out.println("Philosopher " + id + " pick up left");
                   myTable.getLeft(id);
                   Thread.sleep((int)(Math.random()*10));
                   System.out.println("Philosopher " + id + " pick up right");
                   myTable.getRight(id);
                   //eat
                   System.out.println("Philosopher " + id + " eats. Iteration "+ i);
                   Thread.sleep((int)(Math.random()*100));
                   //release chopsticks
                   System.out.println("Philosopher " + id + " drop left"); 
                   myTable.releaseLeft(id);
                   Thread.sleep((int)(Math.random()*10));
                   System.out.println("Philosopher " + id + " drop right");
                   myTable.releaseRight(id);
               } catch (InterruptedException ex) {
                    Logger.getLogger(Philosopher.class.getName()).log(Level.SEVERE, null, ex);
               }           
            }
         }
      }
  </code></pre>
  
  <p>I am completely unable to spot when the so-called: deadlock occurs in my code. I've been trying to figure it out when it occurs, with no result. I am also implementing wait() &amp; notify() and lock unlock calls. </p>
  
  <p>If someone knows this better than me, please tell me. I appreciate all of your help. Thanks a lot in advanced. </p>
  
  <p><strong>EDIT: Updated the code as one sir here required me to do.</strong> </p>
  
  <p><strong>3nd EDIT: This is my output.</strong> </p>
  
  <p>Philosopher 0 thinks. Iteration 0</p>
  
  <p>Philosopher 4 thinks. Iteration 0</p>
  
  <p>Philosopher 3 thinks. Iteration 0</p>
  
  <p>Philosopher 2 thinks. Iteration 0</p>
  
  <p>Philosopher 1 thinks. Iteration 0</p>
  
  <p>Philosopher 1 pick up left</p>
  
  <p>Philosopher 1 pick up right</p>
  
  <p>Philosopher 1 eats. Iteration 0</p>
  
  <p>Philosopher 1 drop right</p>
  
  <p>Philosopher 1 drop left</p>
  
  <p>Philosopher 1 thinks. Iteration 1</p>
  
  <p>Philosopher 4 pick up left</p>
  
  <p>Philosopher 4 pick up right</p>
  
  <p>...</p>
  
  <hr>
  
  <p>Any help on this code is appreciated. If someone can explain to me, I'd really appreciate your help and the stuff you teach me. </p>
  
  <p>New modification of code:</p>
  
  <pre><code>public synchronized void getRight(int n) throws InterruptedException {
          //philosopher n picks up its right chopstick
          int pos = n + 1;
          if (pos == nbrOfChopsticks) {
              pos = 0;
          }
  
          while(!chopstick[pos]){
              wait(); 
          }
  
          chopstick[pos] = false;
      }
  </code></pre>
  
  <p><strong>Adding wait(); and notify(); to the code makes it <em>LAG</em>, see screenshot:</strong></p>
  
  <p><a href="http://imgur.com/PqAY1mR" rel="nofollow">http://imgur.com/PqAY1mR</a></p>