-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathPriority.java
More file actions
57 lines (50 loc) · 1.44 KB
/
Copy pathPriority.java
File metadata and controls
57 lines (50 loc) · 1.44 KB
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
import java.util.TreeSet;
public class Priority extends Algorithm {
private GrantChart grantChart;
public Priority(Process[] processes) {
super(processes);
}
@Override
protected void sort() {
if (sorted_processes == null) {
sorted_processes = new TreeSet<>(new PriorityComparator());
} else {
sorted_processes.clear();
}
for (Process process : processes) {
if (process.getArrivingTime() <= time
&& !grantChart.isProcessStarted(process)) {
sorted_processes.add(process);
}
}
}
@Override
protected void apply() {
grantChart = new GrantChart(processes);
time = 0;
total_waiting_time = 0;
average_waiting_time = 0;
sort();
do {
grantChart.schedule(sorted_processes.first());
grantChart.passTime(sorted_processes.first().getBurstTime());
time = grantChart.getTime();
sort();
} while (sorted_processes.size() > 0);
grantChart.calculateWaitingTime();
grantChart.print();
average_waiting_time = grantChart.getTotalWaitingTime() / processes.length;
}
@Override
public void printResult() {
if (sorted_processes == null) {
apply();
}
System.out.println();
System.out.println(Process.getHeader(true));
for (Process process : processes) {
System.out.println(process.toStringWithPriority());
}
System.out.printf("%s Average waiting time: %.2f\n", getName(), average_waiting_time);
}
}