ariestamirra

rr

May 22nd, 2014
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.56 KB | None | 0 0
  1. package Schedulling.roundRobin;
  2.  
  3. import Schedulling.CpuScheduler;
  4. import java.util.ArrayList;
  5. import Schedulling.Process;
  6.  
  7. /**
  8.  *
  9.  * @author Mirra Ariesta Amalia Adiba
  10.  */
  11. public class RoundRobin extends CpuScheduler{
  12.    
  13.     private int quantumTime = 0;
  14.  
  15.     public int getQuantumTime() {
  16.         return quantumTime;
  17.     }
  18.  
  19.     public void setQuantumTime(int quantumTime) {
  20.         this.quantumTime = quantumTime;
  21.     }
  22.        
  23.     public RoundRobin(int quantumTime) {
  24.         this.quantumTime = quantumTime;
  25.     }    
  26.  
  27.     public RoundRobin() {
  28.     }        
  29.  
  30.     public ArrayList<Process> getQueue(){
  31.         ArrayList<Process> result = new ArrayList<Process>(getListProcess());
  32.        
  33.         for (int i = 0; i < result.size(); i++) {
  34.             for (int j = i+1; j < result.size(); j++) {
  35.                 if(result.get(i).getArrivalTime()>result.get(j).getArrivalTime()){
  36.                     Process temp = result.get(i);
  37.                     result.set(i, result.get(j));
  38.                     result.set(j, temp);
  39.                 }
  40.             }
  41.         }
  42.        
  43.         return result;
  44.     }
  45.    
  46.    
  47.     public Process getNextProcess(ArrayList<Process> processList,Process before){
  48.         if(getRemainingBurstTime()>0 || processList.size()<=0){
  49.             int index = processList.indexOf(before);
  50.             do{
  51.                 index++;
  52.                 if(index>processList.size()-1 || index<0){
  53.                     index=0;
  54.                 }
  55.             }while(processList.get(index).getCurrentBurstTime()<=0);
  56.             return processList.get(index);
  57.         }
  58.         return null;
  59.     }
  60.    
  61.     @Override
  62.     protected void execute() {
  63.         time = 0;
  64.         int currentQuantumTime = quantumTime;
  65.         ArrayList<Schedulling.Process> queue;
  66.         Process before = null;
  67.         while (getRemainingBurstTime() > 0) {
  68.            
  69.             if (getCurrent()!=null && (getCurrent().getCurrentBurstTime() <= 0 || currentQuantumTime<=0)) {  
  70.                 before = getCurrent();
  71.                 detach();
  72.                 currentQuantumTime = quantumTime;
  73.             }
  74.  
  75.             if (getCurrent() == null) {                
  76.                 attach(getNextProcess(getQueue(),before));                
  77.             }
  78.  
  79.             if (getCurrent() != null) {
  80.  
  81.                 getCurrent().doProcess();
  82.             }
  83.  
  84.             queue = getProcessQueue(getCurrent());
  85.             queueWait(queue);
  86.             time++;
  87.             currentQuantumTime--;
  88.         }
  89.         detach();
  90.     }        
  91.        
  92. }
Advertisement
Add Comment
Please, Sign In to add comment