好的,我有两个课程,我的主要是Process
和一个名为Scheduler
的课程
目标:基本上创建一个进程数组,以及剩余的时间。调用计划类,它基本上从timeRemaining
这是主要方法:
public static void main(String[] args) {
// TODO code application logic here
Random rn = new Random();
Scheduler scheduler = new Scheduler();
for(int i = 1; i<=5; i++){
double rand = rn.nextInt(10);
Process process = new Process(i,rand);
scheduler.addObj(process); // adds the object to the array
}
scheduler.printQueue(scheduler.getList());
System.out.println("");
scheduler.sortQueue(scheduler.getList());
scheduler.printQueue(scheduler.getList());
for(int i =0; i <10; i++){
System.out.println("");
scheduler.scheduleNext(scheduler.getList());
scheduler.printQueue(scheduler.getList());
System.out.println("");
}
}
这创建了5个存储在对象数组中的进程。然后我按照剩余时间的降序对数组进行排序。这完全没问题。
问题是从0到10的循环。我正在尝试做的是调用方法scheduleNext()选择哪个接受数组,然后选择哪个值来发送调度方法。
这是scheduleNext()方法:
public void scheduleNext(ArrayList<Process> list){
Process firstElement = list.get(0);
if (firstElement.schedule(firstElement.getTimeRemaining()) == true){
list.remove(0);
}
else{
Collections.rotate(list,-1);
}
}
这是计划方法:
public boolean schedule(double timeRemaining){
if(timeRemaining < 1){
this.timeRemaining = 0;
return true;
}
else{
this.timeRemaining = timeRemaining -1;
return false;
}
}
我已经多次调试它,它会减少timeRemaining并将其移动到数组的底部。但是当我将它打印出来时,它只打印出正常值......我是否正确地重新分配它们?
以下是整个代码(流程类)
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package process;
import java.util.Random;
/**
*
* @author Luke
*/
public class Process implements Comparable<Process> {
/**
* @param args the command line arguments
*/
private int processId;
private double timeRequired;
private double timeRemaining;
public Process(int processId, double timeRequired) {
this.processId = processId;
this.timeRequired = timeRequired;
this.timeRemaining = timeRequired;
}
public double getTimeRemaining() {
return timeRemaining;
}
public static void main(String[] args) {
// TODO code application logic here
Random rn = new Random();
Scheduler scheduler = new Scheduler();
for(int i = 1; i<=5; i++){
double rand = rn.nextInt(10);
Process process = new Process(i,rand);
scheduler.addObj(process);
}
scheduler.printQueue(scheduler.getList());
System.out.println("");
scheduler.sortQueue(scheduler.getList());
scheduler.printQueue(scheduler.getList());
for(int i =0; i <10; i++){
System.out.println("");
scheduler.scheduleNext(scheduler.getList());
scheduler.printQueue(scheduler.getList());
System.out.println("");
}
}
public void setProcessId(int processId) {
this.processId = processId;
}
public void setTimeRequired(double timeRequired) {
this.timeRequired = timeRequired;
}
public int getProcessId() {
return processId;
}
public double getTimeRequired() {
return timeRequired;
}
@Override
public int compareTo(Process o) {
if (this.timeRequired < o.timeRequired){
return -1;
}else if (this.timeRequired > o.timeRequired){
return 1;
}else{
return 0;
}
}
public boolean schedule(double timeRemaining){
if(timeRemaining < 1){
this.timeRemaining = 0;
return true;
}
else{
this.timeRemaining = timeRemaining -1;
return false;
}
}
@Override
public String toString() {
return "Process{" + "processId=" + processId + ", timeRequired=" + timeRequired + '}';
}
}
(计划程序类)
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package process;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
*
* @author Luke
*/
public class Scheduler {
ArrayList<Process> list = new ArrayList<>();
public ArrayList<Process> getList() {
return list;
}
public void addObj(Process p){
list.add(p);
}
public void sortQueue(ArrayList<Process> list){
Collections.sort(list);
}
public void printQueue(ArrayList<Process> list){
for(Process i: list){
System.out.println(i);
}
}
public void scheduleNext(ArrayList<Process> list){
Process firstElement = list.get(0);
if (firstElement.schedule(firstElement.getTimeRemaining()) == true){
list.remove(0);
}
else{
Collections.rotate(list,-1);
}
}
}
答案 0 :(得分:0)
嗯,代码运行正常。您必须为timeRequired
打印错误的timeRemaining
,这会被减少。