Switch to full style
General Java code examples
Post a reply

Shortest Job First Preemptive algorithm

Wed Jan 23, 2013 9:02 pm

Shortest Job First Preemptive algorithm implementation using java
java code
/*
* Scheduler.java
*
* Created on November 19, 2006, 7:46 PM

*/

package sjfscheduler;

/**
*
* @author samu
*/
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;
public class Scheduler {
int numberOfProcesses;

private ArrayList processes = new ArrayList();
private ArrayList inQueue = new ArrayList();
private ArrayList Not_In_Queue_Yet = new ArrayList();
private Proccess_Comperator BusrtCom= new Proccess_Comperator();
private Proccess_Comperator_Arr BusrtCom_Arr= new Proccess_Comperator_Arr();


public void addProcess_Prim(myProcess p)
{
if(p.getArrivalTime()==0)
inQueue.add(p);
else
Not_In_Queue_Yet.add(p);
numberOfProcesses++;
}
public void schedule_SJF_Premitive()
{
int Time=0;
int startProcessing=0;
Collections.sort( inQueue,BusrtCom);
Collections.sort( Not_In_Queue_Yet,BusrtCom_Arr);
myProcess processNow=null;
myProcess processnew=null;
while(true)
{
Time+=1;

if(inQueue.size()!=0)
{
processNow=(myProcess)inQueue.get(0);

if(processNow.getBurstTime()==1)
{
System.out.println(processNow.getName()+" executed from: "+startProcessing+"--> "+Time );
startProcessing=Time;
inQueue.remove(0);
}
else
processNow.decBurstTime();
}
if(Not_In_Queue_Yet.size()!=0)
{
processnew=(myProcess)Not_In_Queue_Yet.get(0);
if(processnew.getArrivalTime()==Time)
{
if(processNow.getBurstTime()>processnew.getBurstTime())
{
System.out.println(processNow.getName()+" executed from: "+startProcessing+"--> "+Time );
startProcessing=Time;
}
inQueue.add(processnew.clone());
Not_In_Queue_Yet.remove(0);
Collections.sort( inQueue,BusrtCom);
}
}
if(Not_In_Queue_Yet.size()==0&&inQueue.size()==0)break;

}
}
public static void main(String[] args){
//Create processes
Scanner inKeyboard=new Scanner(System.in);
int arrivalTime;
int burstTime;
String name;
int numberOf_process;
System.out.println("Plz enter the number of Process : ");
numberOf_process=inKeyboard.nextInt();

Scheduler scheduler = new Scheduler();
for(int i=1;i<=numberOf_process;i++)
{
System.out.println("Plz enter the arrival time of P"+i);
arrivalTime=inKeyboard.nextInt();
System.out.println("Plz enter the burstTime time of P"+i);
burstTime=inKeyboard.nextInt();
scheduler.addProcess_Prim(new myProcess(arrivalTime,burstTime,"P"+i));
}

scheduler.schedule_SJF_Premitive();
}
}
//The class of the process
class myProcess{
private int arrivalTime;
private int burstTime;
private String name;
public myProcess(int arriveTime, int burstTime , String name) {
this.arrivalTime = arriveTime;
this.burstTime = burstTime;
this.name = name;
}
//Get the arrive time value
public int getArrivalTime() {
return arrivalTime;
}
//Set the arrive time value
public void setArrivalTime(int arrivalTime) {
this.arrivalTime = arrivalTime;
}
//Get the burst time value
public int getBurstTime() {
return burstTime;
}
//Set the burst time value
public void setBurstTime(int burstTime) {
this.burstTime = burstTime;
}
public void decBurstTime() {
burstTime-=1;
}
//Get the process name
public String getName() {
return name;
}
//Set the process name
public void setName(String name) {
this.name = name;
}
public myProcess clone()
{
return new myProcess(arrivalTime,burstTime,name);
}
}
class Proccess_Comperator implements Comparator {

/** Creates a new instance of Proccess_Comperator */
public Proccess_Comperator() {
}

public int compare(Object o1, Object o2)
{
int deff= ((myProcess)o1).getBurstTime()-((myProcess)o2).getBurstTime();

if(deff<0)return -1;
if(deff>0)return 1;
return 0;
}

}
class Proccess_Comperator_Arr implements Comparator {

/** Creates a new instance of Proccess_Comperator */
public Proccess_Comperator_Arr() {
}

public int compare(Object o1, Object o2)
{
int deff= ((myProcess)o1).getArrivalTime()-((myProcess)o2).getArrivalTime();

if(deff<0)return -1;
if(deff>0)return 1;
return 0;
}

}




Post a reply
  Related Posts  to : Shortest Job First Preemptive algorithm
 shortest path algorithm java     -  
 source code for SJF and priority for both preemptive C#     -  
 difference between preemptive scheduling and time slicing     -  
 SHORTEST JOB FIRST     -  
 Dijkstra ( Shortest Path )     -  
 Java code to draw shortest path tree     -  
 id3 algorithm     -  
 Generic Algorithm     -  
 Dijkstra Algorithm     -  
 Rijndael Algorithm     -  

Topic Tags

Java Algorithms