代码Java通过选择来实现时间片轮转调度、优先数调度、最短进程优先、最短剩余时间优先四种调度方法
时间: 2023-10-09 15:04:42 浏览: 163
四种进程调度算法FCFS、时间片轮换、优先级调度、最短作业优先.txt
以下是Java代码实现四种调度方法:
1. 时间片轮转调度:
```
import java.util.LinkedList;
import java.util.Queue;
public class TimeSliceSchedule {
public static void main(String[] args) {
int[] processTime = {10, 5, 8, 12};
int timeSlice = 5;
int totalTime = 0;
Queue<Integer> queue = new LinkedList<>();
for (int i = 0; i < processTime.length; i++) {
queue.offer(processTime[i]);
totalTime += processTime[i];
}
System.out.println("时间片轮转调度算法:");
System.out.println("进程执行顺序如下:");
while (!queue.isEmpty()) {
int time = Math.min(queue.peek(), timeSlice);
totalTime -= time;
System.out.print("P" + (processTime.length - queue.size() + 1) + " ");
queue.poll();
if (totalTime > 0) {
queue.offer(time);
}
}
}
}
```
2. 优先数调度:
```
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class PrioritySchedule {
public static void main(String[] args) {
int[] processTime = {10, 5, 8, 12};
int[] priority = {2, 3, 1, 4};
List<Process> list = new ArrayList<>();
for (int i = 0; i < processTime.length; i++) {
list.add(new Process(i + 1, processTime[i], priority[i]));
}
System.out.println("优先数调度算法:");
System.out.println("进程执行顺序如下:");
Collections.sort(list, new Comparator<Process>() {
@Override
public int compare(Process p1, Process p2) {
return p2.priority - p1.priority;
}
});
for (Process p : list) {
System.out.print("P" + p.id + " ");
}
}
static class Process {
int id;
int time;
int priority;
public Process(int id, int time, int priority) {
this.id = id;
this.time = time;
this.priority = priority;
}
}
}
```
3. 最短进程优先:
```
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class ShortestJobFirstSchedule {
public static void main(String[] args) {
int[] processTime = {10, 5, 8, 12};
List<Process> list = new ArrayList<>();
for (int i = 0; i < processTime.length; i++) {
list.add(new Process(i + 1, processTime[i]));
}
System.out.println("最短进程优先调度算法:");
System.out.println("进程执行顺序如下:");
Collections.sort(list, new Comparator<Process>() {
@Override
public int compare(Process p1, Process p2) {
return p1.time - p2.time;
}
});
for (Process p : list) {
System.out.print("P" + p.id + " ");
}
}
static class Process {
int id;
int time;
public Process(int id, int time) {
this.id = id;
this.time = time;
}
}
}
```
4. 最短剩余时间优先:
```
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class ShortestRemainingTimeFirstSchedule {
public static void main(String[] args) {
int[] processTime = {10, 5, 8, 12};
int currentTime = 0;
List<Process> list = new ArrayList<>();
for (int i = 0; i < processTime.length; i++) {
list.add(new Process(i + 1, processTime[i]));
}
System.out.println("最短剩余时间优先调度算法:");
System.out.println("进程执行顺序如下:");
while (!list.isEmpty()) {
Collections.sort(list, new Comparator<Process>() {
@Override
public int compare(Process p1, Process p2) {
return p1.time - p2.time;
}
});
for (int i = 0; i < list.size(); i++) {
Process p = list.get(i);
if (p.time <= currentTime) {
System.out.print("P" + p.id + " ");
list.remove(i);
currentTime += p.time;
break;
}
}
}
}
static class Process {
int id;
int time;
public Process(int id, int time) {
this.id = id;
this.time = time;
}
}
}
```
阅读全文