Skip to content

Instantly share code, notes, and snippets.

@rlingineni
Created April 26, 2017 22:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rlingineni/6d6fb9b970a08bd96f0964f91ffbb277 to your computer and use it in GitHub Desktop.
Save rlingineni/6d6fb9b970a08bd96f0964f91ffbb277 to your computer and use it in GitHub Desktop.
Java implementation of a ShortestRemainingScheduler that takes in a list of Jobs
public class Job {
public Job(String n, int e, int d)
{
name = n;
entryTime = e;
duration = d;
serviceTime = duration;
}
public String name; //Process name
public int entryTime; //time entered
public int duration; //time for process completion
public int serviceTime; //keeps track of how long its waiting
public int waitingTime; //keeps track of how long its waiting
public int priorityNum;
public boolean isProcessing = false; //keeps track of whether somethign has been picked up
public boolean hasMoved = false;
public boolean hasProcessed = false; //uses to make sure its complete or not
}
import java.util.LinkedList;
import java.util.List;
public class ShortestRemainingScheduler {
List<Job> jobsList = new LinkedList<Job>();
int time = 0;
public ShortestRemainingScheduler(List<Job> jList)
{
jobsList = jList;
//run the first process
Run(0);
}
public int select()
{
int nextElement = getNextNotProcessed();
if(nextElement != -1)
{
Job shortest = jobsList.get(nextElement);
int counter = 0;
int index = 0;
for(Job j:jobsList) //iterate all jobs
{
if(!j.hasProcessed) //check if we processed
{
if(time >= j.entryTime) //make sure we can pick it up
{
if(j.duration <= shortest.duration)
{
shortest = j; //get the shortest
index = counter;
}
}
}
counter++;
}
return index;
}else
{
return -1;
}
}
public void Run(int i)
{
if(i != -1)
{
Job j = jobsList.get(i);
System.out.print(j.name);
j.duration -= 1;
time += 1;
if(j.duration <= 0)
{
j.hasProcessed = true;
}
//select the next shortest process
Run(select());
}
}
public int getNextNotProcessed()
{
int counter = 0;
for(Job j:jobsList)
{
if(!j.hasProcessed)
{
return counter;
}
counter++;
}
return -1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment