经过数小时的思考,我终于崩溃了,结果我不知道如何在 java 中实现循环。我尝试了不同的方法,并且我得到了最接近的方法..我用一个例子来解释..
AT = Arrival Time
BT = Burst Time (Execution Time)
起初我有这一行数字(0,5;6,9;6,5;15,10)
,其中位置元素0-2-4
表示到达时间,位置元素1-3-5
表示突发时间。到目前为止,我的代码已将此输入转换为一个名为 Process 的类,它带有一个构造函数:Process(String name, int AT, int BT)
。我已经在ArrayList
. 所以现在我有一个ArrayList alst = [P0,P1,P2,P3]
where P0
hasAT 0
等等BT 5
`..
我创建了一个方法,它将返回一个进程列表,这些进程已经被切割了一段时间 - 例如,如果(0,5;6,9;6,5;15,10)
我会得到一个结果:[P0,P0,P1,P1,P1,P2,P2,P3,P3,P3,P3]
所以轮询方法是一种方法,每个进程都获得执行的量子时间,我选择了 3。
- 带有 AT 0 和 BT 3 的 P0 进入 - 添加到最终列表(经过的时间 = 3)
- 带有 AT 0 和 BT 2 的 P0 进入 - 添加到最终列表(经过的时间 = 5)
- P0 完成
- 具有 AT 6 和 BT 3 的 P1 进入 - 添加到最终列表(经过的时间 = 9)
- 下一个 P1 被添加到队列中
- 具有 AT 6 和 BT 3 的 P2 进入 - 添加到最终列表(经过的时间 = 12)
- 下一个 P2 被添加到队列中
- 具有 AT 6 和 BT 3 的 P1 从队列中进入 - 添加到最终列表(经过的时间 = 15)
- 下一个 P1 进入队列
- P3 到达,添加到最终列表(经过的时间 = 18)
- P1 来自队列 - 添加到最终列表
这就是我觉得我的想法已经崩溃并且我不知道如何排队的地方。
结果应如下所示:[P0,P0,P1,P2,P1,P3,P2,P1,P3,P3,P3]
我根据给出的第一个答案编码的内容。还是不行。。
public ArrayList roundRobinJarjestus(ArrayList pstlst) {
ArrayList queue = new ArrayList();// järjekord, alguses tühi
ArrayList uuspst = new ArrayList();
queue.add(pstlst.get(0));
int i = 0;
double time = 0;
double pworkTime = 0;
int kvant = 3;
while (i < pstlst.size()) {
Protsess p = (Protsess) queue.get(i); //first process is taken
pworkTime = p.getTooaeg(); //execute time
time = time + pworkTime;
// if next arrival time is lower than time passed
if (((Protsess) pstlst.get(i + 1)).getSaabumisaeg() < time) {
queue.add(pstlst.get(i + 1));
}
// if worktime - quantum is higher than zero
// and still left something to execute
if (pworkTime - kvant > 0) {
p.setTooaeg(pworkTime - kvant);
queue.add(p);
}
uuspst.add(queue.get(i));
i = i + 1;
}
return uuspst;
}