-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.java
More file actions
60 lines (52 loc) · 2.03 KB
/
App.java
File metadata and controls
60 lines (52 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/**
* This class represents a dining philosophers simulation.
* It allows multiple philosophers to dine using the provided forks
* and ensures that they follow the dining protocol to avoid deadlock.
* The program waits for user input to stop the simulation.
**/
import java.util.Scanner;
public class App {
private static volatile boolean running = true; // Flag to control execution
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("****************************************************************");
System.out.print("---> Enter the number of philosophers: ");
int philosopherCount = scanner.nextInt();
InterfaceFork[] forks = new InterfaceFork[philosopherCount];
Philosopher[] philosophers = new Philosopher[philosopherCount];
Thread[] threads = new Thread[philosopherCount];
// Create forks
for (int i = 0; i < forks.length; i++) {
forks[i] = new LockBasedFork(i);
}
// Create philosophers
for (int i = 0; i < philosophers.length; i++) {
InterfaceFork leftFork = forks[i];
InterfaceFork rightFork = forks[(i + 1) % philosopherCount];
philosophers[i] = new Philosopher(i + 1, leftFork, rightFork);
threads[i] = new Thread(philosophers[i]);
}
// Start threads
for (Thread thread : threads) {
thread.start();
}
// Wait for input to stop the program
System.out.println("Press enter to stop the program...");
scanner.nextLine();
// Set running flag to false
running = false;
// Wait for threads to finish
try {
for (Thread thread : threads) {
thread.join();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
scanner.close();
}
// Method to check if program should continue running
public static boolean isRunning() {
return running;
}
}