-
Notifications
You must be signed in to change notification settings - Fork 0
/
SJF.java~
76 lines (66 loc) · 1.74 KB
/
SJF.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
/**
* SJF scheduling algorithm.
*/
import java.util.*;
public class SJF implements Algorithm
{
private List<Task> queue;
private Task currentTask;
private List<Task> finishedTasks;
public SJF(List<Task> queue) {
this.queue = queue;
this.finishedTasks = new ArrayList<Task>(queue.size());
}
public void schedule() {
System.out.println("SJF Scheduling \n");
while (!queue.isEmpty()) {
currentTask = pickNextTask();
CPU.run(currentTask, currentTask.getBurst());
}
}
public Task pickNextTask() {
Task temp;
Task nextTask = null;
int minTask = Integer.MAX_VALUE;
for(int i = 0; i<queue.size(); i++){
temp = queue.get(i);
if(temp.getBurst()<minTask){
minTask = temp.getBurst();
nextTask = temp;
}
}
if (currentTask != null){
finishedTasks.add(currentTask);
}
if (queue.size() == 1){
finishedTasks.add(queue.get(0));
}
queue.remove(nextTask);
return nextTask;
}
public double getAverageWaitTime() {
int waitDuration = 0;
Task temp;
int totalTasks = finishedTasks.size();
for(int i=totalTasks-1; i>=0; i--){
temp = finishedTasks.remove(0);
finishedTasks.add(temp);
waitDuration = waitDuration + (temp.getBurst()*i);
}
return waitDuration/totalTasks;
}
public double getAverageResponseTime() {
return this.getAverageWaitTime();
}
public double getAverageTurnaroundTime() {
int turnaround = 0;
Task temp;
int totalTasks = finishedTasks.size();
for(int i=totalTasks; i>0; i--){
temp = finishedTasks.remove(0);
finishedTasks.add(temp);
turnaround = turnaround + (temp.getBurst()*i);
}
return turnaround/totalTasks;
}
}