Class Philosopher

java.lang.Object
  |
  +--Process
        |
        +--Philosopher
All Implemented Interfaces:
java.lang.Runnable

public class Philosopher
extends Process

A Philosopher is a process that thinks and eats. When a philosopher starts to eat, it must first get its left and right chopsticks.


Field Summary
static int EATING
          EATING is the state constant for eating philosophers.
static int HUNGRY
          HUNGRY is the state constant for hungry philosophers.
protected  Philosopher left
          left is the left neighbor of this philosopher.
protected  java.lang.String leftChopstick
          leftChopstick is the name of the chopstick to the left of this philosopher.
protected static Semaphore mutex
          mutex is a mutual exclusion semaphore that is used by all philosophers when accessing philosopher state information.
protected  Semaphore permission
          permission controls permission to eat for this philosopher.
protected  Philosopher right
          right is the right neighbor of this philosopher.
protected  int state
          state is the state of this philosopher: THINKING, HUNGRY, or EATING.
static int THINKING
          THINKING is the state constant for thinking philosophers.
 
Constructor Summary
Philosopher(java.lang.String nm, double t)
          new Philosopher(nm, t) returns a new philosopher and starts executing its run method in a new thread.
 
Method Summary
protected  void eat()
          eat() simulates eating for this philosopher.
 java.lang.String getLeftChopstick()
          phil.getLeftChopstick(p) returns the name of the chopstick to the left of phil.
static void go()
          Philosopher.go() allows philosophers to access their state information.
protected  void pickUpChopsticks()
          pickUpChopsticks() simulates picking up chopsticks for this philosopher.
protected  void putDownChopsticks()
          putDownChopsticks() simulates putting down chopsticks for this philosopher.
 void setLeftNeighbor(Philosopher p)
          phil.setLeftNeighbor(p) makes p the left neighbor of phil and makes phil the right neighbor of p.
protected  void step()
          phil.step() simulates a think and eat cycle for phil.
protected  void think()
          think() simulates thinking for this philosopher.
protected static void tryToGrantPermission(Philosopher p)
          tryToGrantPermission(p) attempts to grant permission for p to eat.
 
Methods inherited from class Process
run, setSimulationTime, toString, twiddle
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

THINKING

public static final int THINKING
THINKING is the state constant for thinking philosophers.

HUNGRY

public static final int HUNGRY
HUNGRY is the state constant for hungry philosophers.

EATING

public static final int EATING
EATING is the state constant for eating philosophers.

state

protected int state
state is the state of this philosopher: THINKING, HUNGRY, or EATING.

left

protected Philosopher left
left is the left neighbor of this philosopher.

right

protected Philosopher right
right is the right neighbor of this philosopher.

permission

protected Semaphore permission
permission controls permission to eat for this philosopher.

leftChopstick

protected java.lang.String leftChopstick
leftChopstick is the name of the chopstick to the left of this philosopher.

mutex

protected static Semaphore mutex
mutex is a mutual exclusion semaphore that is used by all philosophers when accessing philosopher state information. This semaphore should be initialized to 0 to keep philosophers from executing critical code before their neighbors have been set up. The go() method should be invoked after the neighbors have been set up.
Constructor Detail

Philosopher

public Philosopher(java.lang.String nm,
                   double t)
new Philosopher(nm, t) returns a new philosopher and starts executing its run method in a new thread. The name of the new philosopher is formed by concatenating its class name with nm. The average thinking or eating time for the new philosopher is t. The name of its permission semaphore is "Permission " concatenated with nm. The name of the chopstick to its left is "Chopstick " concatenated with nm. A philosopher is born in the thinking state.
Method Detail

setLeftNeighbor

public void setLeftNeighbor(Philosopher p)
phil.setLeftNeighbor(p) makes p the left neighbor of phil and makes phil the right neighbor of p.

getLeftChopstick

public java.lang.String getLeftChopstick()
phil.getLeftChopstick(p) returns the name of the chopstick to the left of phil.

step

protected void step()
phil.step() simulates a think and eat cycle for phil.
Overrides:
step in class Process

think

protected void think()
think() simulates thinking for this philosopher.

pickUpChopsticks

protected void pickUpChopsticks()
pickUpChopsticks() simulates picking up chopsticks for this philosopher.

eat

protected void eat()
eat() simulates eating for this philosopher.

putDownChopsticks

protected void putDownChopsticks()
putDownChopsticks() simulates putting down chopsticks for this philosopher.

tryToGrantPermission

protected static void tryToGrantPermission(Philosopher p)
tryToGrantPermission(p) attempts to grant permission for p to eat. Permission is only granted if p's left and right neighbors are not eating.

go

public static void go()
Philosopher.go() allows philosophers to access their state information. This method must be invoked to start a simulation. It should not be invoked before neighbors have been set up for all philosophers.