1. 程式人生 > >Java實現程序排程演算法(一) FCFS(先來先服務)

Java實現程序排程演算法(一) FCFS(先來先服務)

FCFS類(主類)

只有calc()中涉及了演算法,init()和printResult()都只有簡單的輸入輸出操作。

 1 package xqy.algorithm;
 2 
 3 import java.util.ArrayList;
 4 import java.util.Scanner;
 5 
 6 import xqy.Util.Tools;
 7 import xqy.been.Process;
 8 
 9 /**
10  * @author xqy
11  * @date 2018年12月19日19:15:01
12  * @description 對先到的任務先處理,後到的任務後處理。
13 */ 14 15 public class FCFS { 16 private int processNumber; 17 private ArrayList<Process> processList; 18 19 public FCFS() { 20 init(); 21 calc(); 22 Tools.printResult(processList); 23 } 24 25 private void init() { 26 Scanner sc = new Scanner(System.in);
27 28 System.out.print("<FCFS> Please enter the process num:"); 29 processNumber = sc.nextInt(); 30 31 processList = new ArrayList<Process>(); 32 for (int i = 0; i < processNumber; i++) { 33 processList.add(new Process()); 34 } 35 36
System.out.println("<FCFS> Please enter each process arrival time:"); 37 for (int i = 0; i < processNumber; i++) { 38 System.out.print(" Process" + (i + 1) + ":"); 39 processList.get(i).setArrivalTime(sc.nextInt()); 40 } 41 42 System.out.println("<FCFS> Please enter each process service time:"); 43 for (int i = 0; i < processNumber; i++) { 44 System.out.print(" Process" + (i + 1) + ":"); 45 processList.get(i).setServicesTime(sc.nextInt()); 46 } 47 } 48 49 private void calc() { 50 int timeNow = 0; 51 Process opProcess; 52 53 for (int i = 0; i < processNumber; i++) { 54 opProcess = processList.get(i); 55 56 int waitTime = timeNow - opProcess.getArrivalTime(); 57 int completionTime = timeNow + opProcess.getServicesTime(); 58 int turnAroundTime = completionTime 59 - opProcess.getArrivalTime(); 60 double turnAroundTimeWithWeight = (double) turnAroundTime 61 / opProcess.getServicesTime(); 62 63 opProcess.setStartTime(timeNow); 64 opProcess.setWaitTime(waitTime); 65 opProcess.setCompletionTime(completionTime); 66 opProcess.setTurnAroundTime(turnAroundTime); 67 opProcess.setTurnAroundTimeWithWeight( 68 turnAroundTimeWithWeight); 69 70 timeNow += opProcess.getServicesTime(); 71 } 72 } 73 74 public static void main(String [] args) { 75 new FCFS(); 76 } 77 }

Process類

模擬了程序,對屬性進行了封裝。

 1 package xqy.been;
 2 
 3 public class Process {
 4     private int arrivalTime;
 5     private int servicesTime;
 6     private int remainServiceTime;
 7     private int startTime;
 8     private int waitTime;
 9     private int completionTime;
10     
11     /**
12      * turnAroundTime = completionTime - arrivalTime
13      */
14     private int turnAroundTime;
15     
16     /**
17      * turnAroundTimeWithWeight = turnAroundTime / servicesTime
18      */
19     private double turnAroundTimeWithWeight;
20     
21     public Process() {
22         ;
23     }
24 
25     public int getArrivalTime() {
26         return arrivalTime;
27     }
28 
29     public void setArrivalTime(int arrivalTime) {
30         this.arrivalTime = arrivalTime;
31     }
32 
33     public int getServicesTime() {
34         return servicesTime;
35     }
36 
37     public void setServicesTime(int servicesTime) {
38         this.servicesTime = servicesTime;
39     }
40 
41     public int getRemainServiceTime() {
42         return remainServiceTime;
43     }
44 
45     public void setRemainServiceTime(int remainServiceTime) {
46         this.remainServiceTime = remainServiceTime;
47     }
48 
49     public int getStartTime() {
50         return startTime;
51     }
52 
53     public void setStartTime(int startTime) {
54         this.startTime = startTime;
55     }
56 
57     public int getWaitTime() {
58         return waitTime;
59     }
60 
61     public void setWaitTime(int waitTime) {
62         this.waitTime = waitTime;
63     }
64 
65     public int getCompletionTime() {
66         return completionTime;
67     }
68 
69     public void setCompletionTime(int completionTime) {
70         this.completionTime = completionTime;
71     }
72 
73     public int getTurnAroundTime() {
74         return turnAroundTime;
75     }
76 
77     public void setTurnAroundTime(int turnAroundTime) {
78         this.turnAroundTime = turnAroundTime;
79     }
80 
81     public double getTurnAroundTimeWithWeight() {
82         return turnAroundTimeWithWeight;
83     }
84 
85     public void setTurnAroundTimeWithWeight(double turnAroundTimeWithWeight) {
86         this.turnAroundTimeWithWeight = turnAroundTimeWithWeight;
87     }
88 
89     @Override
90     public String toString() {
91         return "Process [arrivalTime=" + arrivalTime + ", servicesTime="
92                 + servicesTime + ", remainServiceTime=" + remainServiceTime
93                 + ", startTime=" + startTime + ", waitTime=" + waitTime
94                 + ", completionTime=" + completionTime + ", turnAroundTime="
95                 + turnAroundTime + ", turnAroundTimeWithWeight="
96                 + turnAroundTimeWithWeight + "]";
97     }
98 }

Tools類

因為我這次作業要實現幾個類似的演算法,所以我把每個演算法中都要用到的方法都提取出來作為單獨的工具類。

也可以將這些工具方法都放入FCFS類中。

 1 package xqy.Util;
 2 
 3 import java.util.ArrayList;
 4 
 5 import xqy.been.Process;
 6 
 7 public class Tools {
 8 
 9     public static double calcAverageTurnAroundTime(
10             ArrayList<Process> processList) {
11         double sum = 0;
12         for (int i = 0; i < processList.size(); i++) {
13             sum += processList.get(i).getTurnAroundTime();
14         }
15         return Math.round(sum / processList.size() * 100) / 100.0;
16     }
17 
18     public static double calcAverageTurnAroundTimeWithWeight(
19             ArrayList<Process> processList) {
20         double sum = 0;
21         for (int i = 0; i < processList.size(); i++) {
22             sum += processList.get(i).getTurnAroundTimeWithWeight();
23         }
24         return Math.round(sum / processList.size() * 100) / 100.0;
25     }
26 
27     public static void printResult(ArrayList<Process> processList) {
28         System.out.println("\n    #RESULT#");
29 
30         System.out.print("\tArrive:\t\t");
31         for (int i = 0; i < processList.size(); i++) {
32             System.out.print(processList.get(i).getArrivalTime() + "\t");
33         }
34         System.out.println();
35 
36         System.out.print("\tService:\t");
37         for (int i = 0; i < processList.size(); i++) {
38             System.out.print(processList.get(i).getServicesTime() + "\t");
39         }
40         System.out.println();
41 
42         System.out.print("\tStart:\t\t");
43         for (int i = 0; i < processList.size(); i++) {
44             System.out.print(processList.get(i).getStartTime() + "\t");
45         }
46         System.out.println();
47 
48         System.out.print("\tWait:\t\t");
49         for (int i = 0; i < processList.size(); i++) {
50             System.out.print(processList.get(i).getWaitTime() + "\t");
51         }
52         System.out.println();
53 
54         System.out.print("\tFinish:\t\t");
55         for (int i = 0; i < processList.size(); i++) {
56             System.out.print(processList.get(i).getCompletionTime() + "\t");
57         }
58         System.out.println();
59 
60         System.out.print("\tTurn around:\t");
61         for (int i = 0; i < processList.size(); i++) {
62             System.out.print(processList.get(i).getTurnAroundTime() + "\t");
63         }
64         System.out.println();
65 
66         System.out.print("\tTA wight:\t");
67         for (int i = 0; i < processList.size(); i++) {
68             System.out.print(Math.round(processList.get(i)
69                     .getTurnAroundTimeWithWeight() * 100) / 100.0 + "\t");
70         }
71         System.out.println();
72 
73         System.out.println("\tAverage turn around time:"
74                 + Tools.calcAverageTurnAroundTime(processList) + "\t");
75         System.out.println("\tAverage turn around time with wight:"
76                 + Tools.calcAverageTurnAroundTimeWithWeight(processList));
77         
78         System.out.println();
79     }
80 }