用Java实现的轮转调度算法

编程入门 行业动态 更新时间:2024-10-10 14:28:38
本文介绍了用Java实现的轮转调度算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在braining小时,我终于崩溃了,纷纷前来结果是我不知道如何实现循环赛到Java。我尝试过不同的方法和最近我有......嗯,我举个例子来说。

AT =到达时间 BT =突发时间(执行时间)

起初,我有这样的行号(0,5; 6,9; 6,5; 15,10),其中位置的元素 0-2-4  再present到达时间和元素位置 1-3-5 重present破裂时间。  我的code是到目前为止,这个输入变成一类,称为Process附带了一个  构造函数:进程(字符串名称,诠释AT,INT BT)。我已经与在的ArrayList 分离过程。  所以,现在我有一个 ArrayList的alst = [P0,P1,P2,P3],其中P0有在0和BT 5等 ..

我创建了一个将返回我已经切断与时间的量子进程列表的方法 - 例如如果的(0,5; 6,9; 6,5; 15,10)我会得到一个结果: [P0, P0,P1,P1,P1,P2,P2,P3,P3,P3,P3]

因此​​循环赛的方法是一种方法,其中每一道工序得到的量子的时间,我已经选择3执行。

  • P0与AT 0安培; BT 3进来 - 添加至最终名单(时间的流逝 = 3)
  • P0与AT 0安培; BT 2进来 - 添加至最终名单(时间的流逝 = 5)
  • P0完成
  • P1与AT 6安培; BT 3进来 - 添加至最终名单(时间的流逝 = 9)
  • 下一个P1被添加到队列
  • P2与AT 6安培; BT 3进来 - 添加至最终名单(时间的流逝 = 12)
  • 下一个P2被添加到队列
  • P1与AT 6安培; BT 3进来的队列 - 添加至最终名单 (时间的推移= 15)
  • 在接下来的P1进入队列
  • P3到达时,加在最终名单(时间过去了= 18)
  • P1来自队列 - 添加到最终名单
  • 而这正是我觉得我的心一点已经崩溃了,我不知道怎么排队它们。

    结果应该是这样的: [P0,P0,P1,P2,P1,P3,P2,P1,P3,P3,P3]

    编辑:我的$基础上给出的第一个答案C $ CD。仍然无法正常工作。

    公开的ArrayList roundRobinJarjestus(ArrayList中pstlst){     ArrayList的队列=新的ArrayList(); //järjekord,algusestühi     ArrayList的uuspst =新的ArrayList(); //     queue.add(pstlst.get(0));     INT I = 0;     双时间= 0;     双pworkTime = 0;     INT KVANT = 3;     而(ⅰ&其中; pstlst.size()){         Protsess p值=(Pr​​otsess)queue.get(ⅰ); //第一个过程是采取         pworkTime = p.getTooaeg(); //执行时间         时间=时间+ pworkTime;         如果(((Protsess)pstlst.get第(i + 1))getSaabumisaeg()&所述;时间){//如果下一个到达时间比过去的时间低             queue.add(pstlst.get第(i + 1));         }         如果(pworkTime-KVANT大于0){//如果烹饪时间 - 量子大于零,仍然留下一些东西来执行             p.setTooaeg(pworkTime-KVANT);             queue.add(对);         }         uuspst.add(queue.get(ⅰ));         I = I + 1;     }     返回uuspst; }

    解决方案

    您可以保持等待处理的队列,并使用下面的算法:

  • 接第一处理在队列中(如果它不是空的)。将它添加到输出列表。

  • 执行它的时间一给定的量子(或更少,如果剩余时间是小于一个量子),并从该过程的剩余时间减去该量子

  • 如果新的流程已经抵达,将它们添加到队列的末尾。

  • 如果最后执行的处理没有结束(即,它的剩余时间不为0),将其添加到等待队列的末尾。

  • 转到步骤1,如果有剩下的进程。

  • After hours of braining I've finally crashed and have come to result that I have no clue how to implement round robin into java. I've tried different approaches and the closest I've got.. well i explain with an example..

    AT = Arrival Time BT = Burst Time (Execution Time)

    At first i have this row of numbers (0,5;6,9;6,5;15,10) where elements in position 0-2-4 represent arrival times and elements in position 1-3-5 represent burst times. My code is so far that this input is turned into a class, called Process which comes with a constructor: Process(String name, int AT, int BT). I've separated processes with in the ArrayList. So now i have an ArrayList alst = [P0,P1,P2,P3] where P0 has AT 0 and BT 5 and so on..

    I created a method which will return me a list of processes which have been cut with a quantum of time - For example in case of (0,5;6,9;6,5;15,10) i will get a result: [P0,P0,P1,P1,P1,P2,P2,P3,P3,P3,P3]

    So round robin method is a method where every process gets quantum time for execution which I've chosen 3.

  • P0 with AT 0 & BT 3 comes in - added to the final list (time passed = 3)
  • P0 with AT 0 & BT 2 comes in - added to the final list (time passed = 5)
  • P0 finished
  • P1 with AT 6 & BT 3 comes in - added to the final list (time passed = 9)
  • next P1 is added to the queue
  • P2 with AT 6 & BT 3 comes in - added to the final list (time passed = 12)
  • next P2 is added to the queue
  • P1 with AT 6 & BT 3 comes in from queue - added to the final list (time passed = 15)
  • next P1 goes to the queue
  • P3 arrives, added to the final list (time passed = 18)
  • P1 comes from the queue - added to the final list
  • And that's the point where I feel like my mind has crashed and i have no idea how to queue them.

    The result should look like: [P0,P0,P1,P2,P1,P3,P2,P1,P3,P3,P3]

    EDIT: what I coded based on the first answer given. Still doesn't work..

    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 (((Protsess) pstlst.get(i+1)).getSaabumisaeg() < time){ //if next arrival time is lower than time passed queue.add(pstlst.get(i+1)); } if (pworkTime-kvant > 0){ //if worktime - quantum is higher than zero and still left something to execute p.setTooaeg(pworkTime-kvant); queue.add(p); } uuspst.add(queue.get(i)); i = i+1; } return uuspst; }

    解决方案

    You can maintain a queue of waiting processes and use the following algorithm:

  • Pick the first process in the queue(if it is not empty). Add it to the output list.

  • Execute it for a given quantum of time(or less if its remaining time is less then one quantum) and subtract this quantum from the remaining time of this process.

  • If new processes have arrived, add them to the end of the queue.

  • If the last executed process is not finished(that is, its remaining time is not 0), add it to the end of the waiting queue.

  • Go to step 1 if there are any processes left.

  • 更多推荐

    用Java实现的轮转调度算法

    本文发布于:2023-11-30 22:02:58,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1651608.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:算法   Java

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!