Java 作业队列的编写模拟

Java 作业队列的编写模拟,java,linked-list,queue,simulation,Java,Linked List,Queue,Simulation,首先:这不是一个家庭作业,这是我朋友的作业,她已经完成并提交了,我对它的工作方式很好奇,因为我以前从未写过队列模拟,所以决定试一试 基本上,我们得到了一个作业ADT,并且应该编写一个具有常规队列操作(如delete()和insert())的有效队列ADT。使用这两个ADT,我试图编写一个作业队列模拟,其中有一个给定的输入文件,该文件在第1行列出了要处理的作业数,文件中的所有其他行都有一对数字,表示每行上每个作业的到达时间和持续时间。作业比处理器多,作业由(到达时间、持续时间、完成时间)组成 模拟

首先:这不是一个家庭作业,这是我朋友的作业,她已经完成并提交了,我对它的工作方式很好奇,因为我以前从未写过队列模拟,所以决定试一试

基本上,我们得到了一个作业ADT,并且应该编写一个具有常规队列操作(如delete()和insert())的有效队列ADT。使用这两个ADT,我试图编写一个作业队列模拟,其中有一个给定的输入文件,该文件在第1行列出了要处理的作业数,文件中的所有其他行都有一对数字,表示每行上每个作业的到达时间和持续时间。作业比处理器多,作业由(到达时间、持续时间、完成时间)组成

模拟的目标是确定

  • 总等待时间
  • 最长等待时间
  • 平均等待时间 我已经基于
    LinkedList
    编写了一个功能性队列ADT,但是对于如何使用
    作业
    ADT和
    模拟
    文件中的
    队列
    ADT,我感到非常困惑。我知道我的队列ADT可以工作,所以我只是包含了接口文件,以节省空间。我还有一些代码和一些我一直在处理的模拟文件的伪代码。如果我能得到一点推送/一些示例代码,我可能会从那里找到我的方法。请帮忙!:)多谢各位

    输入文件示例:

    3,
    2 2,
    3 4,
    5 6,
    
    input.rpt
    文件(simulation.java创建的报告文件): 报告文件:

    3 Jobs:
    (2, 2, *) (3, 4, *) (5, 6, *) 
    
    ***********************************************************
    1 processor: totalWait=4, maxWait=3, averageWait=1.33
    2 processors: totalWait=0, maxWait=0, averageWait=0.00
    
    input.trc
    文件(simulation.java创建的跟踪文件): 跟踪文件:

    3 Jobs:
    (2, 2, *) (3, 4, *) (5, 6, *) 
    
    *****************************
    1 processor:
    *****************************
    time=0
    0: (2, 2, *) (3, 4, *) (5, 6, *) 
    1: 
    
    time=2
    0: (3, 4, *) (5, 6, *) 
    1: (2, 2, 4) 
    
    time=3
    0: (5, 6, *) 
    1: (2, 2, 4) (3, 4, *) 
    
    time=4
    0: (5, 6, *) (2, 2, 4) 
    1: (3, 4, 8) 
    
    time=5
    0: (2, 2, 4) 
    1: (3, 4, 8) (5, 6, *) 
    
    time=8
    0: (2, 2, 4) (3, 4, 8) 
    1: (5, 6, 14) 
    
    time=14
    0: (2, 2, 4) (3, 4, 8) (5, 6, 14) 
    1: 
    
    *****************************
    2 processors:
    *****************************
    time=0
    0: (2, 2, *) (3, 4, *) (5, 6, *) 
    1: 
    2: 
    
    time=2
    0: (3, 4, *) (5, 6, *) 
    1: (2, 2, 4) 
    2: 
    
    time=3
    0: (5, 6, *) 
    1: (2, 2, 4) 
    2: (3, 4, 7) 
    
    time=4
    0: (5, 6, *) (2, 2, 4) 
    1: 
    2: (3, 4, 7) 
    
    time=5
    0: (2, 2, 4) 
    1: (5, 6, 11) 
    2: (3, 4, 7) 
    
    time=7
    0: (2, 2, 4) (3, 4, 7) 
    1: (5, 6, 11) 
    2: 
    
    time=11
    0: (2, 2, 4) (3, 4, 7) (5, 6, 11) 
    1: 
    2: 
    
    工作广告:

    import java.io.*;
    
    public class Job{
      public static final int UNDEF = -1;
      private int arrival;
      private int duration;
      private int finish;
    
      // default constructor
      public Job(int arrival, int duration){
        this.arrival = arrival;
        this.duration = duration;
        this.finish = UNDEF;
      }
    
      // access functions
      public int getArrival(){return arrival;}
      public int getDuration(){return duration;}
      public int getFinish(){return finish;}
      public int getWaitTime(){
        if( finish==UNDEF ){
           System.err.println("Job: getWaitTime(): undefined finish time");
           System.exit(1);
        }
        return finish-duration-arrival;
       }
    
       // manipulation procedures
      public void computeFinishTime(int timeNow){finish = timeNow + duration;}
      public void resetFinishTime(){finish = UNDEF;}
    
      // toString
      // overrides Object's toString() method
      public String toString(){
        return "("+arrival+", "
                  +duration+", "
                  +(finish==UNDEF?"*":String.valueOf(finish))+")";
      }
    }
    
    队列接口:列出队列ADT执行的操作

    public interface QueueInterface{
    
      // isEmpty()
      // pre: none
      // post: returns true if this Queue is empty, false otherwise
      public boolean isEmpty();
    
      // length()
      // pre: none
      // post: returns the length of this Queue.
      public int length();
    
      // enqueue()
      // adds newItem to back of this Queue
      // pre: none
      // post: !isEmpty()
      public void enqueue(Object newItem);
    
      // dequeue()
      // deletes and returns item from front of this Queue
      // pre: !isEmpty()
      // post: this Queue will have one fewer element
      public Object dequeue() throws QueueEmptyException;
    
      // peek()
      // pre: !isEmpty()
      // post: returns item at front of Queue
      public Object peek() throws QueueEmptyException;
    
      // dequeueAll()
      // sets this Queue to the empty state
      // pre: !isEmpty()
      // post: isEmpty()
      public void dequeueAll() throws QueueEmptyException;
    
      // toString()
      // overrides Object's toString() method
      public String toString();
    }
    
    到目前为止,我在Simulation.java中有哪些内容

    import java.io.*;
    import java.util.Scanner;
    import java.util.Queue;
    import java.util.Job;
    
    public class Simulation{
      public static Job getJob(Scanner in) {
        String[] s = in.nextLine().split(" ");
        int a = Integer.parseInt(s[0]);
        int d = Integer.parseInt(s[1]);
        return new Job(a, d);
      }
      public static void main(String[] args) throws IOException{
        Scanner in = null;
        PrintWriter in.rpt = null;
        PrintWriter in.trc = null;
        in = new Scanner(new File(args[0]);
        in.rpt = new PrintWriter(new FileWriter);
        in.trc = new PrintWriter(new FileWriter);
    
        in.useDelimiter("\n");
        int m = in.next();
        for(int i = 1; i<m; i++){
          System.out.println(getJob(Scanner in));
        } 
        int n = 1;
        while(n<m){
        Queue[] Processors = new Queue[n];
        Processor[0] = new Queue();
        Queue[] Backup = new Queue[n];
    
      // Pseudo code for Simulation
      //    1.  check command line arguments 
      //
      //    2.  open files for reading and writing
      //
      //    3.  read in m jobs from input file
      //
      //    4.  run simulation with n processors for n=1 to n=m-1  {
      //
      //    5.      declare and initialize an array of n processor Queues and any 
      //            necessary storage Queues
      //
      //    6.      while unprocessed jobs remain  { 
      //
      //    7.          determine the time of the next arrival or finish event and 
      //                update time
      //
      //    8.          complete all processes finishing now
      //
      //    9.          if there are any jobs arriving now, assign them to a processor 
      //                Queue of minimum length and with lowest index in the queue array.
      //
      //    10.     } end loop
      //
      //    11.     compute the total wait, maximum wait, and average wait for 
      //            all Jobs, then reset finish times
      //
      //    12. } end loop
      //
      //    13. close input and output files
    
    import java.io.*;
    导入java.util.Scanner;
    导入java.util.Queue;
    导入java.util.Job;
    公共类模拟{
    公共静态作业getJob(扫描仪输入){
    字符串[]s=in.nextLine().split(“”);
    inta=Integer.parseInt(s[0]);
    intd=Integer.parseInt(s[1]);
    返回新工作(a、d);
    }
    公共静态void main(字符串[]args)引发IOException{
    扫描仪输入=空;
    PrintWriter in.rpt=null;
    PrintWriter in.trc=null;
    in=新扫描仪(新文件(args[0]);
    in.rpt=新的PrintWriter(新的FileWriter);
    in.trc=新的PrintWriter(新文件编写器);
    in.useDelimiter(“\n”);
    int m=in.next();
    
    对于(inti=1;i,作为Java(我的第一种编程语言)的学习者,我在设计作业和爱好的程序时遇到了类似的心理障碍。因此,我觉得我最近克服类似的课堂互动困惑的经验可能会对OP有所帮助,社区提供的任何答案,即关注设计过程而不是代码片段,都会对OP、其他学习者和我有所帮助

    细分要求:

    • 总等待时间不需要在处理每个作业时存储每个作业的数据。在模拟中维护一个int实例变量,该变量由每个等待的作业按其等待量递增,结束时包含总等待时间
    • 平均等待时间只是将总数除以m,不需要实例变量
    • 最长等待时间是我发现自己需要灵感的问题类型,因此我将详细说明我的过程,希望这将是你需要的“推动”
    示例报告文件提供了有关所需结构的线索,以及触发报告的状态更改。“Time:x”行表示您需要人为地保持时间,该代码段内的所有内容都将循环,包括报告。伪代码步骤6中的循环是最早的,似乎是合理的

    行仅在作业开始或完成时打印到报告中,而不是每次“时钟”递增时打印可能相同的报告。作业在出列时开始,那么空队列是否真的意味着没有作业在处理中?该问题的答案决定了某些片段在逻辑结构中的位置

    Job.getWaitTime()仅在作业启动后返回值。完成时间由Job.computeFinishTime()通过传递作业到达处理器的时间来计算,不需要传递时间来设置完成。仅在正确的时间调用此方法

    我的一个新手错误是将所有等待时间存储在一个数组中,而不是只存储到目前为止看到的最大值。这样一来,就不需要存储更多的时间,模拟应该有一个实例变量来存储它

    如果您取得进展,我会发现在下一个断点处查看代码很有帮助

    另外,我(学习者)对ADT的理解是,队列ADT最好是泛型的,或者是针对作业而不是对象编写的。