Skip to content

Commit decd597

Browse files
committed
feat: Add SpeculativeExecutionScheduling new algorithm with Junit tests
1 parent bcf4034 commit decd597

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.thealgorithms.scheduling;
2+
3+
import java.util.ArrayList;
4+
import java.util.HashMap;
5+
import java.util.List;
6+
import java.util.Map;
7+
8+
/**
9+
* SpeculativeExecutionScheduling runs multiple copies of the same task in parallel,
10+
* picking the first one that finishes successfully. It is used to mitigate the
11+
* impact of stragglers (slow tasks).
12+
*
13+
* Use Case: Big data systems like Hadoop and Spark, where it helps improve job completion time.
14+
*
15+
* @author Hardvan
16+
*/
17+
public final class SpeculativeExecutionScheduling {
18+
19+
static class Task {
20+
String name;
21+
boolean completed;
22+
23+
Task(String name) {
24+
this.name = name;
25+
this.completed = false;
26+
}
27+
28+
void complete() {
29+
this.completed = true;
30+
}
31+
}
32+
33+
private final Map<String, List<Task>> taskGroups;
34+
35+
public SpeculativeExecutionScheduling() {
36+
taskGroups = new HashMap<>();
37+
}
38+
39+
public void addTask(String groupName, String taskName) {
40+
taskGroups.putIfAbsent(groupName, new ArrayList<>());
41+
taskGroups.get(groupName).add(new Task(taskName));
42+
}
43+
44+
public String executeTasks(String groupName) {
45+
List<Task> tasks = taskGroups.get(groupName);
46+
if (tasks == null) {
47+
return null;
48+
}
49+
for (Task task : tasks) {
50+
if (!task.completed) {
51+
task.complete();
52+
return task.name;
53+
}
54+
}
55+
return null;
56+
}
57+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.thealgorithms.scheduling;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertNull;
5+
6+
import org.junit.jupiter.api.BeforeEach;
7+
import org.junit.jupiter.api.Test;
8+
9+
public class SpeculativeExecutionSchedulingTest {
10+
11+
private SpeculativeExecutionScheduling scheduler;
12+
13+
@BeforeEach
14+
public void setup() {
15+
scheduler = new SpeculativeExecutionScheduling();
16+
}
17+
18+
@Test
19+
public void testAddAndExecuteTask() {
20+
scheduler.addTask("Group1", "Task1");
21+
assertEquals("Task1", scheduler.executeTasks("Group1"));
22+
}
23+
24+
@Test
25+
public void testMultipleTasksInGroup() {
26+
scheduler.addTask("Group1", "Task1");
27+
scheduler.addTask("Group1", "Task2");
28+
assertEquals("Task1", scheduler.executeTasks("Group1"));
29+
assertEquals("Task2", scheduler.executeTasks("Group1"));
30+
}
31+
32+
@Test
33+
public void testExecuteAllTasks() {
34+
scheduler.addTask("Group1", "Task1");
35+
scheduler.addTask("Group1", "Task2");
36+
scheduler.executeTasks("Group1");
37+
scheduler.executeTasks("Group1");
38+
assertNull(scheduler.executeTasks("Group1"));
39+
}
40+
41+
@Test
42+
public void testEmptyTaskGroup() {
43+
assertNull(scheduler.executeTasks("Group2"));
44+
}
45+
}

0 commit comments

Comments
 (0)