-
Notifications
You must be signed in to change notification settings - Fork 19.7k
/
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
package com.thealgorithms.scheduling;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.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
*/
public final class GangScheduling {
static class Gang {
String name;
List<String> tasks;
Gang(String name) {
this.name = name;
this.tasks = new ArrayList<>();
}
void addTask(String task) {
tasks.add(task);
}
List<String> getTasks() {
return tasks;
}
}
private final Map<String, Gang> gangs;
public GangScheduling() {
gangs = new HashMap<>();
}
public void addGang(String gangName) {
gangs.putIfAbsent(gangName, new Gang(gangName));
}
public void addTaskToGang(String gangName, String task) {
Gang gang = gangs.get(gangName);
if (gang != null) {
gang.addTask(task);
}
}
public Map<String, List<String>> getGangSchedules() {
Map<String, List<String>> schedules = new HashMap<>();
for (Gang gang : gangs.values()) {
schedules.put(gang.name, gang.getTasks());
}
return schedules;
}
}