title: Implementing round robin scheduling algorithm in Java
body: <p>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..</p>
  <pre><code>AT = Arrival Time
  BT = Burst Time (Execution Time)
  </code></pre>
  <p>At first i have this row of numbers <code>(0,5;6,9;6,5;15,10)</code> where elements in position <code>0-2-4</code>
  represent arrival times and elements in position <code>1-3-5</code> represent burst times.
  My code is so far that this input is turned into a class, called Process which comes with a
  constructor: <code>Process(String name, int AT, int BT)</code>. I've separated processes with in the <code>ArrayList</code>.
  So now i have an <code>ArrayList alst = [P0,P1,P2,P3]</code> where <code>P0</code> has <code>AT 0</code> and <code>BT 5</code> and so on`..</p>
  <p>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 <code>(0,5;6,9;6,5;15,10)</code> i will get a result: <code>[P0,P0,P1,P1,P1,P2,P2,P3,P3,P3,P3]</code></p>
  <p>So round robin method is a method where every process gets quantum time for execution which I've chosen 3.</p>
  <ol>
  <li>P0 with AT 0 &amp; BT 3 comes in - added to the final list  (time passed
  = 3)</li>
  <li>P0 with AT 0 &amp; BT 2 comes in - added to the final list  (time passed
  = 5)</li>
  <li>P0 finished</li>
  <li>P1 with AT 6 &amp; BT 3 comes in - added to the final list  (time passed
  = 9)</li>
  <li>next P1 is added to the queue</li>
  <li>P2 with AT 6 &amp; BT 3 comes in - added to the final list  (time passed
  = 12)</li>
  <li>next P2 is added to the queue</li>
  <li>P1 with AT 6 &amp; BT 3 comes in from queue - added to the final list
  (time passed = 15)</li>
  <li>next P1 goes to the queue</li>
  <li>P3 arrives, added to the final list (time passed = 18)</li>
  <li>P1 comes from the queue - added to the final list</li>
  </ol>
  <p>And that's the point where I feel like my mind has crashed and i have no idea how to queue them.</p>
  <p>The result should look like: <code>[P0,P0,P1,P2,P1,P3,P2,P1,P3,P3,P3]</code></p>
  <p>What I coded based on the first answer given. Still doesn't work..</p>
  <pre class="lang-java prettyprint-override"><code>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 &lt; 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() &lt; time) {
              queue.add(pstlst.get(i + 1));
          }
  
          // if worktime - quantum is higher than zero
          // and still left something to execute
          if (pworkTime - kvant &gt; 0) {
              p.setTooaeg(pworkTime - kvant);
              queue.add(p);
          }
          uuspst.add(queue.get(i));
          i = i + 1;
      }
      return uuspst;
  }
  </code></pre>