-
Notifications
You must be signed in to change notification settings - Fork 1
/
DiningSimulation.java
107 lines (90 loc) · 3.75 KB
/
DiningSimulation.java
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import java.util.ArrayList;
import java.util.List;
import java.util.Date;
import java.util.concurrent.*;
class DiningSimulation {
private static final int NUM_PHILOSOPHERS_PER_TABLE = 5;
private static final int NUM_TABLES = 6;
private List<Table> tables;
private Table sixthTable;
private ExecutorService deadlockChecker;
private char currentPhilosopherLabel = 'A';
private Date startTime, EndTime;
public DiningSimulation() {
tables = new ArrayList<>();
for (int i = 0; i < NUM_TABLES - 1; i++) {
tables.add(createTable(i));
}
sixthTable = new Table(NUM_PHILOSOPHERS_PER_TABLE, 6);
tables.add(sixthTable);
deadlockChecker = Executors.newFixedThreadPool(NUM_TABLES);
}
private Table createTable(int tableNum) {
Table table = new Table(NUM_PHILOSOPHERS_PER_TABLE, tableNum);
for (int i = 0; i < NUM_PHILOSOPHERS_PER_TABLE; i++) {
Fork leftFork = table.getLeftFork(i);
Fork rightFork = table.getRightFork(i);
Philosopher philosopher = new Philosopher(i, leftFork, rightFork, table, currentPhilosopherLabel);
table.addPhilosopher(philosopher, i);
currentPhilosopherLabel++;
}
return table;
}
public void start() {
startTime = new Date();
System.out.println("Simulation starts at : " + startTime + '\n');
for (Table table : tables) {
table.startDinner();
}
// Start concurrent deadlock detection for all tables
for (int i = 0; i < tables.size() - 1; i++) {
final int tableIndex = i;
deadlockChecker.submit(() -> monitorTableForDeadlock(tables.get(tableIndex)));
}
// Monitor the sixth table for deadlock
deadlockChecker.submit(() -> monitorSixthTableForDeadlock(tables.get(NUM_TABLES - 1)));
}
private void monitorTableForDeadlock(Table table) {
while (true) {
if (table.detectDeadlock()) {
Philosopher movedPhilosopher = table.handleDeadlock();
// Find a random available seat at the sixth table
Table sixthTable = tables.get(NUM_TABLES - 1);
synchronized (sixthTable) {
int index = sixthTable.addPhilosopherToRandomSeat(movedPhilosopher);
sixthTable.startPhilosopherAtIndex(index);
}
break;
}
try {
Thread.sleep(1000); // Check for deadlocks every second
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private void monitorSixthTableForDeadlock(Table sixthTable) {
while (true) {
if (sixthTable.detectDeadlock()) {
EndTime = new Date();
System.out.println("In sixth table, Deadlock detected at " + EndTime + '\n');
// Output the philosopher who last moved to the sixth table
System.out.println("Simulation ends after " + (EndTime.getTime() - startTime.getTime()) + " milliseconds.\n");
Philosopher lastPhilosopher = sixthTable.getLastMovedPhilosopher();
System.out.println("Last philosopher to move to the sixth table: " + lastPhilosopher.getLabel());
sixthTable.stopDinner(); // stop all philosopher
break; // Stop the simulation
}
try {
Thread.sleep(1000); // Check for deadlock every second
} catch (InterruptedException e) {
e.printStackTrace();
break;
}
}
shutdown();
}
private void shutdown() {
deadlockChecker.shutdownNow();
}
}