Question: The Dining Philosophers is a classic problem in computer science posed originally by Edsger Dijkstra as a question for his graduate students in 1965. The
The Dining Philosophers is a classic problem in computer science posed originally by Edsger Dijkstra as a question for his graduate students in 1965. The problem demonstrates some challenges of synchronizing threads and avoiding deadlock. The Problem Five philosophers sit silently around a table with a bowl of spaghetti in front of them. There is a fork to the left and right of each philosopherfive forks in totalone fork between each adjacent pair of philosophers. Independently, the philosophers cycle between thinking and eating. Philosophers can eat only after they pick up the two forks closest to them (on their left and right). A fork can be held by only one philosopher at a time. Philosophers must put down both forks when finished eating. There is an endless supply of spaghetti, and the philosophers never get full. See also https://en.wikipedia.org/wiki/Dining_philosophers_problem As stated there is no way to guarantee that deadlock will not occur. Using a Java 14 JDK or higher, create a non-deadlocking version of the Dining Philosopher Problem. Your implementation shall incorporate the Arbitrator solution as described on the Wikipedia page. No other variations will be accepted. Keep in mind that the critical section for a philosopher is when it checks if its forks are available and possibly picks them up. The waiter (Arbitrator) doesnt actually grant permission to pick up the forks. The waiter just ensures that only one philosopher at a time checks and picks up its forks. Requirements: Philosophers shall be implemented as java.lang.Runnable objects. The Runnable.run() method needs a while loop that allows a philosopher to repeatedly think and eat. The while condition is true when the philosopher is instantiated, but must be changeable from outside the class using an accessor method, public void setStopFlag(). Philosophers must not think or eat inside the critical section. Inside the Runnable.run() method a philosopher needs to accumulate and record several data items. These data need to be saved in private instance variables and accessible from outside the class by public accessor methods. The data include: total loop iterations total number of times a philosopher eats accumulated time spent waiting for the waiter or processing the critical section total time spent in the run method Time durations can be in milliseconds (System.currentTimeMilis() or nanoseconds (System.nanoTime()). A Main or Driver object (its main method) should instantiate and initialize all data structures, in particular forks, philosophers, and threads. After starting each thread (Thread.start()), main should sleep for a number of milliseconds as specified on the command line. When the main thread wakes up, it should signal each thread to stop (do not interrupt) and wait for the thread to complete. After all threads have been joined, main should print summary data as follows.
Philosopher: Alpha Thoughts: 36, Meals: 11 Critical section time (ns): 73334 Total time (ns): 10938677 Time Ratio -- Critical section / Total: 0.0067
Philosopher: Bravo Thoughts: 28, Meals: 3 Critical section time (ns): 40115 Total time (ns): 11119427 Time Ratio -- Critical section / Total: 0.0036
Philosopher: Charlie Thoughts: 29, Meals: 9 Critical section time (ns): 45989 Total time (ns): 11183193 Time Ratio -- Critical section / Total: 0.0041
Philosopher: Delta Thoughts: 33, Meals: 11 Critical section time (ns): 53637 Total time (ns): 10909225 Time Ratio -- Critical section / Total: 0.0049
Philosopher: Echo Thoughts: 28, Meals: 14 Critical section time (ns): 34222 Total time (ns): 10809084 Time Ratio -- Critical section / Total: 0.0032
All instance and static variables shall be private. If you need to access a variable from another class, write an accessor method (getter or setter).
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
