欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java实现进程调度算法(一) FCFS(先来先服务)

程序员文章站 2022-03-24 11:41:13
FCFS类(主类) 只有calc()中涉及了算法,init()和printResult()都只有简单的输入输出操作。 Process类 模拟了进程,对属性进行了封装。 Tools类 因为我这次作业要实现几个类似的算法,所以我把每个算法中都要用到的方法都提取出来作为单独的工具类。 也可以将这些工具方法 ......

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 }