Programming 2 with Java

Exercise: Dining Philosophers Problem

Five philosophers sit around a circular table eating and thinking. In front of each philosopher is a plate and between every two plates is a fork (so there are only five forks). When a philosopher wants to eat, he picks up the forks to the right and left of his plate. When he has finished eating, he puts both forks back on the table. The problem is to ensure that no deadlock occurs and that no philosopher will starve.

Philosophers

Implement the Fork class of the Philosophers program that simulates the dining philosophers problem.

Solution