Question: The task is the Dining Philosophers problem which is useful for studying deadlock. All modifications that you need to do can be done in the

The task is the Dining Philosophers problem which is useful for studying deadlock. All modifications that you need to do can be done in the class Table. Compile and run the code for DiningPhilosophers that is given in Seminar2.zip. Can you detect
in the written output that too many chopsticks are used?
Implement a version where a philosopher waits if a chopstick is already used by another
philosopher. (You can use synchronized or Semaphores.) In this task each philosopher should
first pick up the left and then the right chopstick.
Run the code a couple of times until you observe deadlock. (If it is hard to get a deadlock, modify the times in sleep.
Implement a deadlock free implementation. Hint! Which of the four conditions for a deadlock do you think is the simplest to solve? How can you describe this in Java code?
Final observations
Before you present your seminar, make sure that you have the answer to the questions given in the preparations. Also write down the following for each task to discuss during the seminar.
How did you implement the task?
Why did you solve it in the way you did it?
What difference in behaviour did you notice? package philosophers;
public class DiningPhilosophers {
private static final int NUMBER_OF_PHILOSOPHERS =5;
public static void main(String[] args){
setupTableAndPhilosophers(NUMBER_OF_PHILOSOPHERS);
}
private static void setupTableAndPhilosophers(int numberOfPhilosophers)
{
Table table = new Table(numberOfPhilosophers);
for (int i =0; i < numberOfPhilosophers; i++){
Thread thread = new Thread(new Philosopher(i, table));
thread.start();
}
}
} package philosophers;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Philosopher implements Runnable{
private int myId;
private Table myTable;
public Philosopher(int id,Table table){
myId = id;
myTable = table;
}
@Override
public void run(){
for(int i =0 ; i <100; i++){
try {
System.out.println("Philosopher "+ myId +" thinks. Iteration "+ i);
Thread.sleep((int)(Math.random()*100));
myTable.getLeftChopstick(myId);
System.out.println("Philosopher "+ myId +" pick up left");
Thread.sleep((int)(Math.random()*10));
myTable.getRightChopstick(myId);
System.out.println("Philosopher "+ myId +" pick up right");
System.out.println("Philosopher "+ myId +" eats. Iteration "+ i);
Thread.sleep((int)(Math.random()*100));
myTable.releaseLeftChopstick(myId);
System.out.println("Philosopher "+ myId +" drop left");
Thread.sleep((int)(Math.random()*10));
myTable.releaseRightChopstick(myId);
System.out.println("Philosopher "+ myId +" drop right");
Thread.sleep((int)(Math.random()*10));
} catch (InterruptedException ex){
Logger.getLogger(Philosopher.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
} package philosophers;
public class Table {
private int nbrOfChopsticks;
private boolean chopstick[]; // true if chopstick[i] is available
public Table(int nbrOfSticks){
nbrOfChopsticks = nbrOfSticks;
chopstick = new boolean[nbrOfChopsticks];
for (int i =0; i < nbrOfChopsticks; i++){
chopstick[i]= true;
}
}
public void getLeftChopstick(int n){
chopstick[n]= false;
}
public void getRightChopstick(int n){
int pos = n +1;
if (pos == nbrOfChopsticks)
pos =0;
chopstick[pos]= false;
}
public void releaseLeftChopstick(int n){
chopstick[n]= true;
}
public void releaseRightChopstick(int n){
int pos = n +1;
if (pos == nbrOfChopsticks)
pos =0;
chopstick[pos]= true;
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!