title: calculate average waiting time for a preemptive shortest job first scheduling algorithm
body: <p>My target is to calculate average waiting time for a preemptive shortest job first scheduling algorithm.</p>
  
  <p>Assume the arrival times for the jobs are in intervals of 2 units, like 0,2,4,6..... Ie, 1st job enters in 0 units and 2nd job after 2 units of time and so on.</p>
  
  <p>I tested for 3 test cases for my program and got right answers:   </p>
  
  <blockquote>
    <p>Test case 1:<br>
    jobs:8,4,9,5<br>
    avg Time:6.5  </p>
    
    <p>Test case 2:<br>
    jobs:7,4,1,4<br>
    avg Time:3  </p>
  </blockquote>
  
  <p>But when I took a file having 1000 jobs as input I got avg Time:16872.434
  But the code i got from internet gave me answer as avg Time : 16024
  I didn`t understand how to attach that text file here...
  so, I just want to know if my code is right? if not where did I go wrong.?  </p>
  
  <pre><code>package algoritm_W4_M6;
  
  import java.io.FileInputStream;
  import java.io.FileNotFoundException;
  import java.util.PriorityQueue;
  import java.util.Scanner;
  import java.util.Vector;
  /**
   * To calculate the average Waiting Time of jobs when done in shortest Job First(SJF)-Preemptive
   * @author Rahul Konda
   *
   */
  public class Preemptivr_SJV {
      Vector&lt;Float&gt; burstTimes ;
      public Preemptivr_SJV() {
          burstTimes = new Vector&lt;Float&gt;();
      }
      public void loadFile() {
          try {
              float f;
              Scanner scan = new Scanner(new FileInputStream("cpu_burst_times.txt"));
              while(scan.hasNext()) {
                  f = Float.parseFloat( scan.nextLine());             
                  burstTimes.add(f);
              }
          } catch (FileNotFoundException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
      }
      public void readc() {
          burstTimes.add((float) 7);
          burstTimes.add((float) 4);
          burstTimes.add((float) 1);
      //  burstTimes.add((float) 8);
          burstTimes.add((float) 4);
      //  burstTimes.add((float) 2);
      }
      public float calculateAvgWaitingTime() {
  //      readc(); //this is for test cases 1 and 2
          loadFile(); //this is to read from file
          float waitingTime= 0.0f;
          float totalTime = 0.0f;
  
          PriorityQueue&lt;Float&gt; pq = new PriorityQueue&lt;Float&gt;();
  
          for (Float time : burstTimes) {
              pq.add(time);
              Float minTime = pq.poll();
              if(time&lt;=2) {
                  waitingTime = waitingTime +(minTime*pq.size());
                  continue;
              }
              waitingTime = waitingTime +2*pq.size();
              pq.add(minTime-2);//as job not completed I add back to queue
          }
          totalTime = totalTime + waitingTime;    //summing up the above waiting times
          waitingTime = 0.0f;
          while(!pq.isEmpty()) {
              waitingTime = waitingTime +pq.poll();
              totalTime = totalTime + waitingTime;    //summing up the above waiting times
          }
  
          totalTime = totalTime - waitingTime;
          System.out.println("Jobs burst values:\n"+burstTimes.toString());
          return (totalTime/1000);
  
  
  
      }
      public static void main(String[] args) {
          Preemptivr_SJV fs = new Preemptivr_SJV();
          System.out.println("\nAverage Waiting Time is: "+fs.calculateAvgWaitingTime());
      }
  }
  </code></pre>
  
  <p>The above code is in java and thanks in advance.!</p>