The Dining Philosophers


Go watch the philosophers in action. [Try here if you are getting security errors]

Go to a discussion of the design of the applet.

The Scenario

The Dining Philosophers is a classic concurrent programming problem that illustrates the pitfalls of multithreaded applications. Simply put, the Dining Philosophers scenario is five philosophers sitting around a table doing what they do best: thinking and eating. In the middle of the table is a plate of food, and in between each philosopher is a chopstick. The philosophers spend most of their time thinking, but when they get hungry they reach for the two sticks next to them and start eating. A philosopher cannot begin eating until he has both sticks. When he is done eating, he puts the sticks down and continues thinking.

The problems arise when two or more adjacent philosophers get hungry at the same time. Starvation can occur if a philosopher's neighbors hog the sticks, and he can never get two chopsticks to eat. It is also possible that all five philosophers can get hungry simultanesouly and reach to pick up one stick. Then, when they reach for the other stick they see that every philosopher has one stick and no one can get two. This is known as deadlock, and they all starve.

Inspiration

I first saw this algorithm implemented in Java on the CD from the book Hooked on Java. It was a very good example of Java's multithreaded capability, but it did not use objects well. I thought it sorely lacked in the object oriented design that Java was supposed to be promoting. Thus, I redesigned the applet from the ground up, and this is the result.

I did use the outstanding graphics and sound from the book, but everything else, including all code, is original. And, actually, I changed the graphics a little to be more indicative of what each philosopher was up to.


Last update: Septmeber 15, 1997.
David Whitney