Javatpoint Logo
Javatpoint Logo

Dining Philosophers Problem and Solution in Java

The Dining Philosophers Problem is an example of a concurrency problem dealing with the allocation of limited resources among competing processes. In this section, we will understand how to avoid deadlock conditions in dining philosophers problem. It is the undesirable condition of concurrent systems. It is marked as in a circular waiting state. First, we will discuss the Dining Philosophers Problem that is used in the operating systems after that we will move towards the solution. Also, we will implement the solution in a Java program.

Dining Philosophers Problem

The pictorial representation of the problem is as follows.

Dining Philosophers Problem and Solution in Java

The above figure represents that there are five philosophers (labeled with P1, P2, P3, P4, and P5) sitting around a circular dining table. There are five plates of noodles to eat when philosophers feel hungry. To eat noodles, there are five forks/ chopsticks (labeled with 1 to 5) placed between each philosopher.

Each philosopher eats and thinks alternatively. There are the following conditions followed by each philosopher:

  • A philosopher will use both forks/ chopsticks (right and left) to eat.
  • Remaining one fork may be picked up by any one of its adjacent philosophers but not both.
  • A philosopher may have noodles if both forks are available.
  • After eating, a philosopher will put down both forks and starts thinking again.
  • Those forks can be picked by the other philosophers who will repeat the same process.
  • No two neighbor philosophers (right and left) can eat together.

Initially, all philosophers are thinking. After some time, gets hungry and wants to eat noodles. The philosopher looks for the forks on either side. When the philosopher gets both the forks, he starts eating. After eating, he puts down the forks and starts thinking again. When the philosopher puts down the forks, those forks may be used by neighbor philosophers.

In such a case, there is the possibility of deadlock, a condition in which two or more processes cannot continue execution. The problem is used to design a scheduling technique in such a way no philosopher will starve.

The Solution to the Dining Philosophers Problem

The solution to the dining philosophers' problem is to use Semaphore. It is a tool that is used for concurrent processes. There is a drawback of using Semaphore as a solution. It may lead to deadlock. Suppose a scenario when all philosophers pick up the left fork and wait for the right fork. The situation leads to a deadlock.

To avoid the deadlock solution, there are some measures that should have been taken are:

  • There should be at most four philosophers around the table.
  • A philosopher should allow picking forks when both forks are available at the same time.
  • The philosophers can alternatively eat and think. For example, if the first philosopher is eating then the adjacent neighbor philosophers should wait and think, and so on.

Implementation of Dining Philosophers Problem

In the following program, first, we have initialized the number of philosophers (5). The two arrays philosophers[] and chopsticks[] initialized with the number of philosophers (5).

In order to implement the logic for chopsticks, we have created a class named Chopstick. Inside the class, we have created a constructor of the Semaphore class and defined three methods namely grab(), release(), and isFree().

The grab() method invokes the acquire() method that acquires a permit from this semaphore. It reduces the number of permits by 1. If there is no permit is available the current thread becomes disabled.

The user-defined release() method invokes the release() method of the Semaphore class. It releases the given number of permits and increments the permit by 1.

The isFree() method checks for the availability of permits in the semaphore. It invokes the availablePermits() method of the Semaphore class that returns the number of permits available in the Semaphore.

After that, we have created another class named Philosopher that extends the Thread class. Inside the class, we have defined three variables number, leftchopstick, and rightchopstick. Also, we have created a constructor of the Philosopher class and the eat() method. The run() method of the Thread class is called if the thread was constructed using a separate Runnable object.

The run() method executes when a philosopher has both chopsticks (right and left). A philosopher starts eating by invoking the eat() method and holds the fork for a specified time (sleep time). In order to determine the sleep time, we have used the ThreadLocalRandom. current(). nextInt() method. The method returns a pseudorandom, uniformly distributed integer value between 0 and 1000. The value is determined by the nextInt() method passed in the sleep() method that sleeps the thread for a specified time. In our case, if sleep time is more than 1000ms, the program will exit. So, we can set the range of pseudorandom numbers (sleep time).

Inside the main() method, we have defined the two for loops one for chopsticks, and the other for philosophers. After that, we have checked for deadlock conditions. If a deadlock occurs, it means each philosopher is eating by acquiring the chopsticks. The execution of the program breaks. The deadlock condition will not occur until resources (a single chopstick) is available.


Dining Philosophers Problem and Solution in Java

Let's do some minor changes in the above program.

Inside the while loop, change the thread sleep time 1000 to 2000 ms.

Now execute the above program, we get the following output.

Dining Philosophers Problem and Solution in Java

Note: You may get a different output from the above output. Because the output changes each time when we execute the program.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA