Question: Need help with program. deals with the Dining Philosopher problem with mutithreading on Java. two classes given, dining and miscsubs. The dining.java is an empty
Need help with program. deals with the Dining Philosopher problem with mutithreading on Java. two classes given, dining and miscsubs. The dining.java is an empty main program that you will edit to include your implementation. The miscsubs.java class contains a set of subroutines provided by the instructor that will assist you in determining if your implementation is correct, and that will be used for grading. You must compile the class files individually with javac. Use Java threads for each philosopher. Use java synchronize to lock the chopsticks. Use the Java wait and notify functions to implement the condition variables.
dining class:
// Use java threads to simulate the Dining Philosophers Problem
class dining
{
public static void main(String args[])
{
System.out.println("Starting the Dining Philosophers
Simulation ");
miscsubs.InitializeChecking();
// Your code here...
// End of your code
miscsubs.LogResults();
}
};
miscsubs class:
import java.lang.Thread; import java.util.Random;
class miscsubs { static int NUMBER_PHILOSOPHERS = 5; static int NUMBER_CHOPSTICKS = 5; static int MAX_EATS = 500; static int TotalEats = 0; static int EatCount[] = new int[NUMBER_PHILOSOPHERS]; static boolean EatingLog[] = new boolean[NUMBER_PHILOSOPHERS]; static Random r; static void InitializeChecking() { int i; for (i = 0; i < NUMBER_PHILOSOPHERS; ++i) { EatingLog[i] = false; } } static synchronized void StartEating(int MyIndex) { // Un-comment below for debugging.. //System.out.println("Philosopher " + MyIndex + " Eating"); TotalEats++; EatCount[MyIndex]++; EatingLog[MyIndex] = true; int LeftNeighbor = (MyIndex == 0)? NUMBER_PHILOSOPHERS-1:MyIndex-1; int RightNeighbor = (MyIndex + 1) % NUMBER_PHILOSOPHERS; // At no time can any be eating at the same time as neighbors */ if (EatingLog[LeftNeighbor] || EatingLog[RightNeighbor]) { System.out.println("ERROR! Philosopher " + MyIndex + " eating incorrectly"); } } static synchronized void DoneEating(int MyIndex) { EatingLog[MyIndex] = false; } static void LogResults() { for (int i = 0; i < NUMBER_PHILOSOPHERS; i++) { System.out.println("EatCount " + i + " - " + EatCount[i]); } } static void RandomDelay() { if (r == null) { r = new Random(); } int ycount = r.nextInt(900) + 100; for (int i = 0; i < ycount; ++i) { Thread.currentThread().yield(); } } }
If this was successful, the output will be:
Starting the Dining Philosophers Simulation
EatCount 0 - 0?
EatCount 1 0
?EatCount 2 - 0
EatCount 3 - 0
EatCount 4 - 0
note: the program should use one thread to represent one philosopher. they should cycle between thinking, hungry and eating. we have a max of 500 eats and with a total of 5 philosophers, each should eat an equivalent (100 times). program must not burn cpu time. thank you we cannot make changes to the miscsubs.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
