Concurrent Programming with Java
Lab Manual, Version 1.0, F. Astha Ekadiyanto, 2002.

[Contents] [Next] [Previous]

Lab 6: Monitors and Thread Synchronization 2


Dining Philosophers scenario

Note: The codes in this section are modified codes based on Stephan Fischli's Dining Phillosophers Code

A bunch of philosophers (counted 5 in the figure) sit around a circular table. Each philosopher spends his life alternately thinking and eating. In the centre of the table is a large bowl of kebab. A philosopher needs two forks to help eating the kebab. One fork is placed between each pair of philosophers and they agree that each will only use the fork to his immediate right and left.

Each Fork is a shared resource with actions get and put. When hungry, each Philosopher must first get his right and left forks before he can start eating.
Now we will try to implement the scenario in an applet animation.

Create a new BlueJ Project called DiningPhil Project.

First of all we will need image representations of the philosopher's states as the followings:

Thinking

Hungry
Getting Left Fork

Hungry
Getting Right Fork

Hungry
Got Left Fork
Getting Right Fork

Hungry
Got Right Fork
Getting Left Fork
Eating
Got all Forks
Cartoons by Mackie G. Majcher

The above images and addition images can be taken from the diningphil.zip file. Extract them in the Path_to_DiningPhil/img directory.

The final result of the BlueJ Project will have an UML like representation as shown below.

We will code them one by one:


[Contents] [Next] [Previous]