【发布时间】:2015-11-14 23:54:23
【问题描述】:
好的,我有两个主要课程是 Process 和一个名为 Scheduler 的课程
目标:基本上创建一个进程数组,以及它们的剩余时间。调用从 timeRemaining 中删除数字 1 的调度类
这里是主要方法:
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);
}
}
}
【问题讨论】: