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 Seminarzip. 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 NUMBEROFPHILOSOPHERS ;
public static void mainString args
setupTableAndPhilosophersNUMBEROFPHILOSOPHERS;
private static void setupTableAndPhilosophersint numberOfPhilosophers
Table table new TablenumberOfPhilosophers;
for int i ; i numberOfPhilosophers; i
Thread thread new Threadnew Philosopheri table;
thread.start;
package philosophers;
import java.util.loggingLevel;
import java.util.loggingLogger;
public class Philosopher implements Runnable
private int myId;
private Table myTable;
public Philosopherint idTable table
myId id;
myTable table;
@Override
public void run
forint i ; i ; i
try
System.out.printlnPhilosopher myId thinks. Iteration i;
Thread.sleepintMathrandom;
myTable.getLeftChopstickmyId;
System.out.printlnPhilosopher myId pick up left";
Thread.sleepintMathrandom;
myTable.getRightChopstickmyId;
System.out.printlnPhilosopher myId pick up right";
System.out.printlnPhilosopher myId eats. Iteration i;
Thread.sleepintMathrandom;
myTable.releaseLeftChopstickmyId;
System.out.printlnPhilosopher myId drop left";
Thread.sleepintMathrandom;
myTable.releaseRightChopstickmyId;
System.out.printlnPhilosopher myId drop right";
Thread.sleepintMathrandom;
catch InterruptedException ex
Logger.getLoggerPhilosopherclass.getNamelogLevelSEVERE, null, ex;
package philosophers;
public class Table
private int nbrOfChopsticks;
private boolean chopstick; true if chopsticki is available
public Tableint nbrOfSticks
nbrOfChopsticks nbrOfSticks;
chopstick new booleannbrOfChopsticks;
for int i ; i nbrOfChopsticks; i
chopsticki true;
public void getLeftChopstickint n
chopstickn false;
public void getRightChopstickint n
int pos n ;
if pos nbrOfChopsticks
pos ;
chopstickpos false;
public void releaseLeftChopstickint n
chopstickn true;
public void releaseRightChopstickint n
int pos n ;
if pos nbrOfChopsticks
pos ;
chopstickpos true;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
