Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Schedulling;
- import java.util.ArrayList;
- import Schedulling.Process;
- /**
- *
- * @author Mirra Ariesta Amalia Adiba
- */
- public abstract class CpuScheduler {
- public interface CpuSchedulerEvent {
- public abstract void onDetachProcess(Time time);
- }
- protected int time = 0;
- private Process current;
- public ArrayList<Time> processTimeLine = new ArrayList<Time>();
- public void attach(Process current) {
- this.current = current;
- if (current != null) {
- processTimeLine.add(new Time(current.getNama(), time));
- }
- }
- public void detach() {
- if (current != null) {
- processTimeLine.get(processTimeLine.size() - 1).end = time;
- if (event != null) {
- event.onDetachProcess(processTimeLine.get(processTimeLine.size() - 1));
- }
- }
- current = null;
- }
- public Process getCurrent() {
- return current;
- }
- public CpuSchedulerEvent event;
- protected ArrayList<Schedulling.Process> listProcess;
- public CpuScheduler() {
- listProcess = new ArrayList<Schedulling.Process>();
- }
- public void addProses(Process process) {
- listProcess.add(process);
- }
- public ArrayList<Process> getListProcess() {
- return listProcess;
- }
- private double sumWaitingTime() {
- int result = 0;
- for (int i = 0; i < listProcess.size(); i++) {
- result += (listProcess.get(i).getWaitingTime());
- }
- return result;
- }
- protected ArrayList<Process> getProcessQueue(Process except) {
- ArrayList<Process> result = new ArrayList<Process>();
- for (int i = 0; i < listProcess.size(); i++) {
- if (listProcess.get(i).getArrivalTime() <= time && listProcess.get(i).getCurrentBurstTime() > 0) {
- if (listProcess.get(i) != except) {
- result.add(listProcess.get(i));
- }
- }
- }
- return result;
- }
- protected void queueWait(ArrayList<Process> queue) {
- for (int i = 0; i < queue.size(); i++) {
- queue.get(i).doWait();
- }
- }
- protected int getRemainingBurstTime() {
- Process current;
- int result = 0;
- for (int i = 0; i < listProcess.size(); i++) {
- current = listProcess.get(i);
- result += (current.getCurrentBurstTime());
- }
- return result;
- }
- public double countAvg() {
- return sumWaitingTime() / listProcess.size();
- }
- protected abstract void execute();
- @Override
- public String toString() {
- String result = "";
- for (int i = 0; i < listProcess.size(); i++) {
- result += (listProcess.get(i).getNama() + "=" + listProcess.get(i).getWaitingTime() + "\n");
- }
- return result;
- }
- public int sumBurstTime() {
- int total = 0;
- for (int i = 0; i < listProcess.size(); i++) {
- total += listProcess.get(i).getBurstTime();
- }
- return total;
- }
- private int getLastEnd() {
- Time time = null;
- for (int i = 0; i < processTimeLine.size(); i++) {
- if (time == null) {
- time = processTimeLine.get(i);
- } else {
- if (time.end < processTimeLine.get(i).end) {
- time = processTimeLine.get(i);
- }
- }
- }
- return time.end;
- }
- private Time getTimeByStartValue(int start) {
- Time time = null;
- for (int i = 0; i < processTimeLine.size(); i++) {
- time = processTimeLine.get(i);
- if (time.start == start) {
- return time;
- }
- }
- return null;
- }
- private Time getTimeByEndValue(int end) {
- Time time = null;
- for (int i = 0; i < processTimeLine.size(); i++) {
- time = processTimeLine.get(i);
- if (time.end == end) {
- return time;
- }
- }
- return null;
- }
- public String getData() {
- StringBuffer buf = new StringBuffer("Nama\tArrival\tBurstTime\tWaitingTime\n");
- System.out.println("Nama\tArrival\tBurstTime\tWaitingTime");
- for (int i = 0; i < listProcess.size(); i++) {
- Process temp = listProcess.get(i);
- buf.append(temp.getNama() + "\t" + temp.getArrivalTime() + "\t" + temp.getBurstTime() + "\t" + temp.getWaitingTime() + "\n");
- }
- return buf.toString();
- }
- public String getTimelineReport() {
- StringBuffer buf = new StringBuffer();
- buf.append("Nama\tStart\tEnd\n");
- for (int i = 0; i < processTimeLine.size(); i++) {
- Schedulling.Time temp = processTimeLine.get(i);
- buf.append(temp.processName + "\t" + temp.start + "\t" + temp.end+"\n");
- }
- return buf.toString();
- }
- public String getStringChart() {
- String buf = "", axis = "", process = "";
- int n = 0;
- for (int i = 0; i <= getLastEnd(); i++) {
- axis += String.format("%3d ", i);
- buf += ("--- ");
- Time time = getTimeByStartValue(i);
- if (time != null) {
- process += String.format("%3s ",time.processName);
- } else if (i == getLastEnd()) {
- process += String.format("%3s ",getTimeByEndValue(i).processName);
- } else {
- process += String.format("%3s ","");
- }
- }
- return axis+"\n"+buf+"\n"+process+"\n";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment