- Notifications
You must be signed in to change notification settings - Fork 19.9k
/
Copy pathGangScheduling.java
61 lines (50 loc) · 1.46 KB
/
GangScheduling.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
packagecom.thealgorithms.scheduling;
importjava.util.ArrayList;
importjava.util.HashMap;
importjava.util.List;
importjava.util.Map;
/**
* GangScheduling groups related tasks (gangs) to run simultaneously on multiple processors.
* All tasks in a gang are executed together or not at all.
*
* Use Case: Parallel computing environments where multiple threads of a program
* need to run concurrently for optimal performance.
*
* @author Hardvan
*/
publicfinalclassGangScheduling {
staticclassGang {
Stringname;
List<String> tasks;
Gang(Stringname) {
this.name = name;
this.tasks = newArrayList<>();
}
voidaddTask(Stringtask) {
tasks.add(task);
}
List<String> getTasks() {
returntasks;
}
}
privatefinalMap<String, Gang> gangs;
publicGangScheduling() {
gangs = newHashMap<>();
}
publicvoidaddGang(StringgangName) {
gangs.putIfAbsent(gangName, newGang(gangName));
}
publicvoidaddTaskToGang(StringgangName, Stringtask) {
Ganggang = gangs.get(gangName);
if (gang != null) {
gang.addTask(task);
}
}
publicMap<String, List<String>> getGangSchedules() {
Map<String, List<String>> schedules = newHashMap<>();
for (Ganggang : gangs.values()) {
schedules.put(gang.name, gang.getTasks());
}
returnschedules;
}
}