ariestamirra

round

May 25th, 2014
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.85 KB | None | 0 0
  1. package com.raizal.roundRobin;
  2.  
  3. import com.raizal.CpuScheduler;
  4. import java.util.ArrayList;
  5. import com.raizal.Process;
  6.  
  7. public class RoundRobin extends CpuScheduler {
  8.  
  9.     private int quantumTime = 0;
  10.  
  11.     public int getQuantumTime() {
  12.         return quantumTime;
  13.     }
  14.  
  15.     public void setQuantumTime(int quantumTime) {
  16.         this.quantumTime = quantumTime;
  17.     }
  18.  
  19.     public RoundRobin(int quantumTime) {
  20.         this.quantumTime = quantumTime;
  21.     }
  22.  
  23.     public RoundRobin() {
  24.     }
  25.  
  26.     public Process getNextProcess(ArrayList<Process> processList, Process before) {
  27.         if (getRemainingBurstTime() > 0 && processList.size() > 0) {
  28.             int index = processList.indexOf(before);
  29.             do {
  30.                 index++;
  31.                 if (index >= processList.size() || index < 0) {
  32.                     index = 0;
  33.                 }
  34.             } while (processList.get(index).getCurrentBurstTime() <= 0);
  35.             return processList.get(index);
  36.         }
  37.         return null;
  38.     }
  39.  
  40.     @Override
  41.     protected void execute() {
  42.         time = 0;
  43.         int currentQuantumTime = quantumTime;        
  44.         Process before = null;
  45.         while (getRemainingBurstTime() > 0) {
  46.  
  47.             if (getCurrent() != null && (getCurrent().getCurrentBurstTime() <= 0 || currentQuantumTime <= 0)) {
  48.                 before = getCurrent();
  49.                 detach();
  50.                 currentQuantumTime = quantumTime;
  51.             }
  52.  
  53.             if (getCurrent() == null) {
  54.                 attach(getNextProcess(getProcessQueue(null), before));
  55.             }
  56.  
  57.             if (getCurrent() != null) {
  58.  
  59.                 getCurrent().doProcess();
  60.                 currentQuantumTime--;
  61.             }
  62.  
  63.             queueWait(getProcessQueue(getCurrent()));
  64.             time++;            
  65.         }
  66.         detach();
  67.     }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment