title: How to implement SJF scheduling to this code?
body: <p>I am developing code for printer that schedules jobs using Operating System concepts. I am accepting the arrival time and number of pages to be printed. I have used semaphore so that only one job can be printed at a time. How can I implement SJF scheduling to it?
  Here is my code:</p>
  
  <pre><code>import java.util.*;
  import java.util.concurrent.Semaphore;
  public class newproj{
      static boolean b=true;
      public static Semaphore s = new Semaphore(1);
      public static void main(String[] args){
  
          Scanner sc=new Scanner(System.in);
          int n;
          System.out.println("Enter number of print jobs:");
          n=sc.nextInt();
          int a[][]=new int[n][2];
          System.out.println("Enter number of pages of each job and arrival time:");
          for(int i=0;i&lt;n;i++)
          {
              for(int j=0;j&lt;2;j++)
              {
                  a[i][j]=sc.nextInt();
              }
          }
  
          Thread[] threads = new Thread[n];
          for (int i = 0; i &lt; threads.length; i++) {
              threads[i] = new Thread(new user(i,a[i][0],a[i][1],s));
              threads[i].start();
          }
      }
  }
  
      class user extends Thread{
      int a,i=0;
      int n,t,ct=0;
      Semaphore s;
      String st;
      public user(int n,int a,int t,Semaphore s)
      {
          this.n=n;
          this.a=a;
          this.s=s;
          this.t=t;
          //System.out.println(this.n);
      }
      public void run()
      {
      while(true)
          {
              synchronized(this){
              if((newproj.b==true)&amp;&amp;(ct&gt;=t))
              {
              try{
                  s.acquire();
                  newproj.b=false;
                  }
              catch(Exception e){}
                  System.out.println("Printing "+a+" pages of Job "+(n+1)+" "+(ct++));
                  try{
                      for(int j=1;j&lt;=a;j++)
                          System.out.println("Printed "+j+" page of Job "+(n+1)+" "+(ct++));
                      Thread.sleep(1000);
                      }
              catch(Exception e){}
              System.out.println("Job "+(n+1)+" has finished printing "+(ct++));
              newproj.b=true;
              s.release();
              try{
                  Thread.sleep(1000);
                  }
              catch(Exception e){}
              break;
              }
              else if((newproj.b==false)&amp;&amp;(ct&gt;t))
              {
              System.out.println("Job "+(n+1)+" is waiting"+(ct++));
              try{
                  Thread.sleep(1000);
                  }
              catch(Exception e){}                
              }
              else if(ct&lt;t)
              {
              System.out.println("No Job to be executed"+(n+1)+" "+(ct++));
              try{
                  Thread.sleep(1000);
                  }
              catch(Exception e){}                                
              }
              }
              }
          };
  
  }
  </code></pre>