1. 程式人生 > >作業排程演算法(Java)

作業排程演算法(Java)

package Task03;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class App {
	private List<Integer> arr = new ArrayList<Integer>();
	private Scanner sc = new Scanner(System.in);
	private Job_Scheduling[] job_Schedulings = null;
	private int TimeCount = 0;

	public static void main(String[] args) {
		App app = new App();
		app.Initlizer();
		app.Print();
		app.Run();
		app.Print();
		app.TextPrint();
	}

	public App() {
		System.out.print("Please Input The THe Homeworks You REally Want :>>>>>>>>>>\t");
		int temp_count = sc.nextInt();
		job_Schedulings = new Job_Scheduling[temp_count];
	}

	public void Initlizer() {
		System.out.println("-------------------------------Initlizer----------------------------------");
		System.out.println("請輸入作業序號,作業入井時間,需要執行時間>>>>>>>>>>>>\t");
		System.out.println("作業名" + "  作業入井時間" + "  需要執行時間");
		for (int i = 0; i < job_Schedulings.length; i++) {
			Job_Scheduling temp = new Job_Scheduling();
			temp.setName(sc.next());
			temp.setQueueTime(sc.nextInt());
			temp.setRunTime(sc.nextInt());
			job_Schedulings[i] = temp;
		}
	}

	/**
	 * 執行相關方法***********************************************************************
	 */
	public void Run() {

		while (!IsDone()) {
			CanRun();
			int key = ShouldRun();
			if (key == -1) {
				TimeCount++;
			} else {
				Excute(key);
			}
		}
	}

	public boolean IsDone() {
		for (Job_Scheduling temp : job_Schedulings) {
			if (temp.flag != 1) {
				return false;
			}
		}
		return true;
	}

	public void CanRun() {
		for (Job_Scheduling temp : job_Schedulings) {
			if (temp.queueTime <= TimeCount && temp.flag != 1) {
				temp.flag = 0;
			}
		}
	}

	public int ShouldRun() {
		List<Integer> list = new ArrayList<Integer>();
		for (int i = 0; i < job_Schedulings.length; i++) {
			if (job_Schedulings[i].flag == 0) {
				list.add(i);
			}
		}
		int key = Confirm(list);
		return key;
	}

	public int Confirm(List<Integer> list) {
		if (list.isEmpty()) {
			return -1;
		}
		double MaxTemp = -1;
		int key = 0;
		for (int i = 0; i < list.size(); i++) {
			double temp = (TimeCount - job_Schedulings[list.get(i)].queueTime) / job_Schedulings[list.get(i)].runTime;
			if (temp > MaxTemp) {
				MaxTemp = temp;
				key = i;
			}
		}
		return list.get(key);
	}

	public void Excute(int key) {
		job_Schedulings[key].startTime = TimeCount;
		job_Schedulings[key].endTime = TimeCount + job_Schedulings[key].runTime;
		job_Schedulings[key].avg_Time = (job_Schedulings[key].endTime - job_Schedulings[key].queueTime)
				/ job_Schedulings[key].runTime;
		job_Schedulings[key].flag = 1;
		TimeCount = job_Schedulings[key].endTime;
		arr.add(key);
	}

	/**
	 * 執行相關方法***********************************************************************
	 */

	/**
	 * 列印相關方法**************************************************************
	 */
	public void Print() {
		System.out.println("-------------------------------Print----------------------------------");
		for (Job_Scheduling temp : job_Schedulings) {
			System.out.println(temp);
		}
	}

	public void TextPrint() {
		if (arr.isEmpty())
			return;
		int sum = 0;
		System.out.println("--------------------執行順序--------------------->");
		for (int i = 0; i < arr.size(); i++) {
			sum += job_Schedulings[i].avg_Time;
			System.out.print(job_Schedulings[arr.get(i)].getName() + "-->");
		}
		System.out.println();
		System.out.println("---------------------平均週轉時間------------------->");
		System.out.println("--->" + sum / arr.size());
	}
	/**
	 * 列印相關方法**************************************************************
	 */

}

class Job_Scheduling {
	/**
	 * queueTime:作業入井時間
	 */

	private String name;
	public int queueTime;
	public int runTime;
	public int startTime;
	public int endTime;
	public double avg_Time;

	/**
	 * -1表示沒進入佇列 0表示已經進入了就行佇列 1表示執行結束
	 */
	public int flag = -1;

	@Override
	public String toString() {
		return "Job_Scheduling [name=" + name + ", queueTime=" + queueTime + ", runTime=" + runTime + ", startTime="
				+ startTime + ", endTime=" + endTime + ", avg_Time=" + avg_Time + "]";
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setQueueTime(int queueTime) {
		this.queueTime = queueTime;
	}

	public void setRunTime(int runTime) {
		this.runTime = runTime;
	}

	public String getName() {
		return name;
	}

}