mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-13 16:15:31 +08:00
Add SlackTimeScheduling
algorithm (#5814)
This commit is contained in:
@ -539,6 +539,7 @@
|
|||||||
* [PreemptivePriorityScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/PreemptivePriorityScheduling.java)
|
* [PreemptivePriorityScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/PreemptivePriorityScheduling.java)
|
||||||
* [RRScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/RRScheduling.java)
|
* [RRScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/RRScheduling.java)
|
||||||
* [SJFScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/SJFScheduling.java)
|
* [SJFScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/SJFScheduling.java)
|
||||||
|
* [SlackTimeScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/SlackTimeScheduling.java)
|
||||||
* [SRTFScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/SRTFScheduling.java)
|
* [SRTFScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/SRTFScheduling.java)
|
||||||
* searches
|
* searches
|
||||||
* [BinarySearch](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/searches/BinarySearch.java)
|
* [BinarySearch](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/searches/BinarySearch.java)
|
||||||
@ -1134,6 +1135,7 @@
|
|||||||
* [PreemptivePrioritySchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/PreemptivePrioritySchedulingTest.java)
|
* [PreemptivePrioritySchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/PreemptivePrioritySchedulingTest.java)
|
||||||
* [RRSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/RRSchedulingTest.java)
|
* [RRSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/RRSchedulingTest.java)
|
||||||
* [SJFSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/SJFSchedulingTest.java)
|
* [SJFSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/SJFSchedulingTest.java)
|
||||||
|
* [SlackTimeSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/SlackTimeSchedulingTest.java)
|
||||||
* [SRTFSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/SRTFSchedulingTest.java)
|
* [SRTFSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/SRTFSchedulingTest.java)
|
||||||
* searches
|
* searches
|
||||||
* [BinarySearch2dArrayTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/BinarySearch2dArrayTest.java)
|
* [BinarySearch2dArrayTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/BinarySearch2dArrayTest.java)
|
||||||
|
@ -0,0 +1,64 @@
|
|||||||
|
package com.thealgorithms.scheduling;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Comparator;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* SlackTimeScheduling is an algorithm that prioritizes tasks based on their
|
||||||
|
* slack time, which is defined as the difference between the task's deadline
|
||||||
|
* and the time required to execute it. Tasks with less slack time are prioritized.
|
||||||
|
*
|
||||||
|
* Use Case: Real-time systems with hard deadlines, such as robotics or embedded systems.
|
||||||
|
*
|
||||||
|
* @author Hardvan
|
||||||
|
*/
|
||||||
|
public class SlackTimeScheduling {
|
||||||
|
|
||||||
|
static class Task {
|
||||||
|
String name;
|
||||||
|
int executionTime;
|
||||||
|
int deadline;
|
||||||
|
|
||||||
|
Task(String name, int executionTime, int deadline) {
|
||||||
|
this.name = name;
|
||||||
|
this.executionTime = executionTime;
|
||||||
|
this.deadline = deadline;
|
||||||
|
}
|
||||||
|
|
||||||
|
int getSlackTime() {
|
||||||
|
return deadline - executionTime;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private final List<Task> tasks;
|
||||||
|
|
||||||
|
public SlackTimeScheduling() {
|
||||||
|
tasks = new ArrayList<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds a task to the scheduler.
|
||||||
|
*
|
||||||
|
* @param name the name of the task
|
||||||
|
* @param executionTime the time required to execute the task
|
||||||
|
* @param deadline the deadline by which the task must be completed
|
||||||
|
*/
|
||||||
|
public void addTask(String name, int executionTime, int deadline) {
|
||||||
|
tasks.add(new Task(name, executionTime, deadline));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Schedules the tasks based on their slack time.
|
||||||
|
*
|
||||||
|
* @return the order in which the tasks should be executed
|
||||||
|
*/
|
||||||
|
public List<String> scheduleTasks() {
|
||||||
|
tasks.sort(Comparator.comparingInt(Task::getSlackTime));
|
||||||
|
List<String> scheduledOrder = new ArrayList<>();
|
||||||
|
for (Task task : tasks) {
|
||||||
|
scheduledOrder.add(task.name);
|
||||||
|
}
|
||||||
|
return scheduledOrder;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,48 @@
|
|||||||
|
package com.thealgorithms.scheduling;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
public class SlackTimeSchedulingTest {
|
||||||
|
|
||||||
|
private SlackTimeScheduling scheduler;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
public void setup() {
|
||||||
|
scheduler = new SlackTimeScheduling();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testAddAndScheduleSingleTask() {
|
||||||
|
scheduler.addTask("Task1", 2, 5);
|
||||||
|
List<String> expected = List.of("Task1");
|
||||||
|
assertEquals(expected, scheduler.scheduleTasks());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testScheduleMultipleTasks() {
|
||||||
|
scheduler.addTask("Task1", 2, 5);
|
||||||
|
scheduler.addTask("Task2", 1, 4);
|
||||||
|
scheduler.addTask("Task3", 3, 7);
|
||||||
|
List<String> expected = List.of("Task1", "Task2", "Task3");
|
||||||
|
assertEquals(expected, scheduler.scheduleTasks());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testScheduleTasksWithSameSlackTime() {
|
||||||
|
scheduler.addTask("Task1", 2, 5);
|
||||||
|
scheduler.addTask("Task2", 3, 6);
|
||||||
|
scheduler.addTask("Task3", 1, 4);
|
||||||
|
List<String> expected = List.of("Task1", "Task2", "Task3");
|
||||||
|
assertEquals(expected, scheduler.scheduleTasks());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testEmptyScheduler() {
|
||||||
|
List<String> expected = List.of();
|
||||||
|
assertEquals(expected, scheduler.scheduleTasks());
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user