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]
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 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;
}