Class DiningPhilosophers

java.lang.Object
  |
  +--DiningPhilosophers

public class DiningPhilosophers
extends java.lang.Object

Dining philosophers example. The program uses Java threads and locks provided by the synchronized keyword. Output on console -- text mode application. The program terminates when all philosophers have already eaten and thought 5 times. You can redefine the Philosopher.NO_OF_TURNS constant or rewrite the Philosopher.run() method (replace for(...) with while(true)) to change this behavior.

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

Field Summary
private  Fork[] forks
           
private  int noOfLivingPhilosophers
           
private  Philosopher[] philosophers
           
private  int timeToEat
           
private  int timeToThink
           
 
Constructor Summary
DiningPhilosophers()
           
 
Method Summary
private  void createThreads()
          Creates all philosophers.
 void decrementCountOfPhilosophers()
          Decrements the count of philosophers present in the program.
private  void doIt(java.lang.String[] args)
          If command line parameters are accepted, creates philosophers, lets them run and waits until they all finish.
private  void hello()
          Prints out a hello message.
static void main(java.lang.String[] args)
          Runs the program.
private  void printExplanation()
          Prints out an explanation of symbols used during program output.
 void printForks()
          Prints out a line describing the current state of forks and philosophers.
private  boolean setTimes(java.lang.String[] args)
          Processes command line parameters.
private  void usage()
          Prints out a message explaining program's usage.
private  void waitForZeroPhilosophers()
          Blocks until all philosophers finish their work.
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, toString, wait, wait, wait
 

Field Detail

noOfLivingPhilosophers

private int noOfLivingPhilosophers

timeToThink

private int timeToThink

timeToEat

private int timeToEat

forks

private Fork[] forks

philosophers

private Philosopher[] philosophers
Constructor Detail

DiningPhilosophers

public DiningPhilosophers()
Method Detail

main

public static void main(java.lang.String[] args)
Runs the program.

doIt

private void doIt(java.lang.String[] args)
If command line parameters are accepted, creates philosophers, lets them run and waits until they all finish.

hello

private void hello()
Prints out a hello message.

setTimes

private boolean setTimes(java.lang.String[] args)
Processes command line parameters.

printExplanation

private void printExplanation()
Prints out an explanation of symbols used during program output.

createThreads

private void createThreads()
Creates all philosophers.

usage

private void usage()
Prints out a message explaining program's usage.

printForks

public void printForks()
Prints out a line describing the current state of forks and philosophers.

decrementCountOfPhilosophers

public void decrementCountOfPhilosophers()
Decrements the count of philosophers present in the program.

waitForZeroPhilosophers

private void waitForZeroPhilosophers()
Blocks until all philosophers finish their work.