public class Philosopher extends Thread {
  private Chopstick left;
  private Chopstick right;

  Philosopher(String name, Chopstick left, Chopstick right) {
    setName(name);
    this.left = left;
    this.right = right;
  }

  public void run() {
    for(;;) {
      System.out.println(getName()+" is hungry");               

      System.out.println(getName()+" wants left chopstick");
      left.take(getName());
      System.out.println(getName()+" got left chopstick");
      
      try {
	sleep(100);
      } catch(InterruptedException e) {
	System.out.println(e);
      }     
      
      System.out.println(getName()+" wants right chopstick");
      right.take(getName());
      System.out.println(getName()+" got right chopstick");

      System.out.println(getName()+" eats");               
      try {
	sleep((long)(Math.random()*1000));
      } catch(InterruptedException e) {
	System.out.println(e);
      }

      System.out.println(getName()+" finished eating");               
      
      left.release(getName());
      System.out.println(getName()+" released left chopstick");
      
      right.release(getName());
      System.out.println(getName()+" released right chopstick");

      System.out.println(getName()+" thinks");
      try {
	sleep((long)(Math.random()*1000));
      } catch(InterruptedException e) {
	System.out.println(e);
      }
      
      System.out.println(getName()+" finished thinking");               
    }
  }
}
