Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

The Dining

Philosophers Problem in
Java

1. Introduction
The Dining Philosopher Problem states that K philosophers seated around
a circular table with one chopstick between each pair of philosophers.
There is one chopstick between each philosopher. A philosopher may eat
if he can pickup the two chopsticks adjacent to him. One chopstick may be
picked up by any one of its adjacent followers but not both.

2. The Problem

The diagram above represents the


problem. There are five silent philosophers (P1 – P5) sitting around a circular
table, spending their lives eating and thinking.
There are five forks for them to share (1 – 5) and to be able to eat, a philosopher
needs to have forks in both his hands. After eating, he puts both of them down and
then they can be picked by another philosopher who repeats the same cycle.

3. A Solution

An initial solution would be to make each of the philosophers follow the following
protocol:

As the above pseudo code describes, each philosopher is initially thinking. After a
certain amount of time, the philosopher gets hungry and wishes to eat.
At this point, he reaches for the forks on his either side and once he's got both
of them, proceeds to eat. Once the eating is done, the philosopher then puts the
forks down, so that they're available for his neighbor.

You might also like