Class Philosopher

java.lang.Object
  |
  +--java.lang.Thread
        |
        +--Philosopher

public class Philosopher
extends java.lang.Thread

A philosopher spends its life by thinking and eating. To be able to eat, he must get both forks, one on his left side and one on his right side. After finishing eating, he releases both forks. He dies after having completed 5 life cycles. In a real example, the loop would be infinite.

Author:
Jarda Kacer, Czech OS/2 User Group; email: jkacer@kiv.zcu.cz

Field Summary
private  Fork firstFork
           
private  int id
           
private  Fork leftFork
           
static int NO_OF_PHILOSOPHERS
           
static int NO_OF_STATES
           
static int NO_OF_TURNS
           
private  DiningPhilosophers parent
           
private  Fork rightFork
           
private  Fork secondFork
           
private  int state
           
static int STATE_DEAD
           
static int STATE_EATING
           
static int STATE_RELEASING
           
static int STATE_THINKING
           
static int STATE_WAITING
           
static char[] STATES
           
private  int timeToEat
           
private  int timeToThink
           
 
Fields inherited from class java.lang.Thread
contextClassLoader, daemon, eetop, group, inheritedAccessControlContext, MAX_PRIORITY, MIN_PRIORITY, name, NORM_PRIORITY, priority, single_step, stillborn, stopThreadPermission, target, threadInitNumber, threadQ, values
 
Constructor Summary
Philosopher(DiningPhilosophers parent, int id, Fork leftFork, Fork rightFork, int timeToThink, int timeToEat)
          Creates a new philosopher, with a unique ID.
 
Method Summary
private  void eat()
          Suspends the philosopher for a random number of seconds -- simulation of eaiting.
 Fork getLeftFork()
          Returns the philosopher's left fork.
 Fork getRightFork()
          Returns the philosopher's right fork.
 int getState()
          Returns the philosopher's state.
 void run()
          Periodically thinks and eats.
private  void think()
          Suspends the philosopher for a random number of seconds -- simulation of thinking.
 
Methods inherited from class java.lang.Thread
, activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, exit, getContextClassLoader, getName, getPriority, getThreadGroup, init, interrupt, interrupt0, interrupted, isAlive, isDaemon, isInterrupted, isInterrupted, join, join, join, nextThreadNum, registerNatives, resume, resume0, setContextClassLoader, setDaemon, setName, setPriority, setPriority0, sleep, sleep, start, stop, stop, stop0, suspend, suspend0, toString, yield
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

NO_OF_PHILOSOPHERS

public static final int NO_OF_PHILOSOPHERS

NO_OF_STATES

public static final int NO_OF_STATES

NO_OF_TURNS

public static final int NO_OF_TURNS

STATE_THINKING

public static final int STATE_THINKING

STATE_WAITING

public static final int STATE_WAITING

STATE_EATING

public static final int STATE_EATING

STATE_RELEASING

public static final int STATE_RELEASING

STATE_DEAD

public static final int STATE_DEAD

STATES

public static final char[] STATES

id

private int id

state

private int state

leftFork

private Fork leftFork

rightFork

private Fork rightFork

firstFork

private Fork firstFork

secondFork

private Fork secondFork

timeToThink

private int timeToThink

timeToEat

private int timeToEat

parent

private DiningPhilosophers parent
Constructor Detail

Philosopher

public Philosopher(DiningPhilosophers parent,
                   int id,
                   Fork leftFork,
                   Fork rightFork,
                   int timeToThink,
                   int timeToEat)
Creates a new philosopher, with a unique ID.
Method Detail

getState

public int getState()
Returns the philosopher's state.

getLeftFork

public Fork getLeftFork()
Returns the philosopher's left fork.

getRightFork

public Fork getRightFork()
Returns the philosopher's right fork.

think

private void think()
Suspends the philosopher for a random number of seconds -- simulation of thinking.

eat

private void eat()
Suspends the philosopher for a random number of seconds -- simulation of eaiting.

run

public void run()
Periodically thinks and eats. To be able to eat, the philosopher must get both forks. After finishing eating, he releases the forks. For simplicity, the number of loops is limited to 5.
Overrides:
run in class java.lang.Thread