THE EMOTIONAL SIDE OF UX DESIGN

To achieve the desired positive response from a UX Design created to influence the consumers, with respect to a service or product, it is essential to have a unique appeal of the design. UX design…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




The Dining Philosophers Problem in Java

1. Introduction

The Dining Philosophers problem is one of the classic problems used to describe synchronization issues in a multi-threaded environment and illustrate techniques for solving them. Dijkstra first formulated this problem and presented it regarding computers accessing tape drive peripherals.

The dining philosophers problem states that there are 5 philosophers sharing a circular table and they eat and think alternatively. There is a bowl of rice for each of the philosophers and 5 chopsticks. A philosopher needs both their right and a left chopstick to eat. A hungry philosopher may only eat if there are both chopsticks available. Otherwise, a philosopher puts down their chopstick and begin thinking again.

The goal is to come up with a scheme/protocol that helps the philosophers achieve their goal of eating and thinking without getting starved to death.

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 chopsticks 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 chopsticks down, so that they’re available for his neighbor.

This scheme exactly implements the one described earlier: a Philosopher thinks for a while and then decides to eat. After this, he acquires the forks to his left and right and starts eating. When done, he places the forks down. We also add timestamps to each action, which would help us understand the order in which events occur. To kick start the whole process, we write a client that creates 5 Philosophers as threads and starts all of them:

We model each of the forks as generic Java objects and make as many of them as there are philosophers. We pass each Philosopher his left and right forks that he attempts to lock using the synchronized keyword. Running this code results in an output similar to the following. Your output will most likely differ from the one given below, mostly because the sleep() method is invoked for a different interval:

We can confirm the same by running the above code a few times and checking that some times, the code just hangs. Here’s a sample output that demonstrates the above issue:

In this situation, each of the Philosophers has acquired his left fork, but can’t acquire his right fork, because his neighbor has already acquired it. This situation is commonly known as the circular wait and is one of the conditions that results in a deadlock and prevents the progress of the system.

As we saw above, the primary reason for a deadlock is the circular wait condition where each process waits upon a resource that’s being held by some other process. Hence, to avoid a deadlock situation we need to make sure that the circular wait condition is broken. There are several ways to achieve this, the simplest one being the follows:

The change comes in lines 17–19 of the above code, where we introduce the condition that makes the last philosopher reach for his right fork first, instead of the left. This breaks the circular wait condition and we can avert the deadlock. Following output shows one of the cases where all the Philosophers get their chance to think and eat, without causing a deadlock:

Add a comment

Related posts:

Inebriate

The breath is a miasma poisoning the essence of the souls around. “Inebriate” is published by Jyr_o Shen.

Bring it forward.

If you notice something troublesome, conflicting or have a solution for a problem – have the guts to bring it forward. Especially when working in a team it’s important to get your points out there…

Too much knowledge for the entire humanity? How we are going to handle and its Impacts.

Have you ever thought about the books that are written but never read? Well not an exciting thought, right? It could be a dull and boring book and the writer must be so crazy that people simply give…