1

这是帮助解决任何问题的 hwk 链接提示。请阅读以下内容以查看我的问题。这只是了解我的问题的信息

http://penguin.ewu.edu/cscd300/Spring_12/Assignments/prog2.html

使用队列和堆栈,以作业的形式,我给出了一个家庭作业,它基本上接受一个输入文件并编写一个命令参数来计算运行时间。我给出了如下所示的输入文件:

Media Player
100
Angry Birds
140

然后我们在 jgrasp 中输入一个参数,例如“jobs.txt 50 1”,这意味着文本文件、时间片和延迟。(见上面的链接)这是基于上面显示的示例输入的程序执行示例:

C:\classes\cscd300\queues>java QueueSimulation jobs.txt 50 1

Time Slice: 50
Latency: 1

Adding Jobs to Queue
--------------------
Media Player, 100 milliseconds to execute
Angry Birds, 140 milliseconds to execute

Begin Job Processing
--------------------
Executing Media Player, 100 milliseconds remaining
Executing Angry Birds, 140 milliseconds remaining
Executing Media Player, 50 milliseconds remaining
*Media Player finished, total time to execute was 153 milliseconds
Executing Angry Birds, 90 milliseconds remaining
*Angry Birds finished, total time to execute was 244 milliseconds

所有工作已完成!

这是我的代码:(下面有一个作业类、一个测试器和一个链接队列类,我用它来创建自己的入队和出队类(hwk 的一部分,我没有包括它,因为那是很多代码,只要知道入队和出队是方法)我的问题在测试仪中:

    public class Job {
    String jobName;
    int runTime;
    int remainTime;
    int startTime;

//constructures
    public Job() {
    jobName = "";
    runTime = 0;
    remainTime = 0;
    startTime = 0;
    }

    public Job(String _jobName, int _runTime) {
    jobName = _jobName;
    runTime = _runTime;
    remainTime = _runTime;
    startTime = 0;
    }
} 


public class QueueTester {
    public static void main(String[] args) {
        linkedQueue waitQ = new linkedQueue();
        linkedQueue runQ = new linkedQueue();
        String fileName = "no_fileName";
        int slice = 0, timeSW=0;
        if (args.length !=3) 
        throw new IllegalStateException("No command line arguments...\n");
        fileName = args[0];
        slice = Integer.parseInt(args[1]);
        timeSW = Integer.parseInt(args[2]); 
        System.out.println("\nTime Slice: " + slice);
        System.out.println("\nLatency: " +timeSW + "\n");
        Job job = null;
        try {
            FileInputStream fstream = new FileInputStream(fileName);
            DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String strLine;
            System.out.println("adding jobs to queue");
            System.out.println("--------------------");
            boolean first = true;
            while ((strLine = br.readLine()) != null) {
                if (job == null)
                    job = new Job("", 0);

                strLine = strLine.trim();
                if (strLine.startsWith("#")) continue;
                if (strLine.length() == 0) continue;

                if (first) {
                    job.jobName = strLine;
                    first = false;
                } else {
                    job.runTime = Integer.parseInt(strLine);
                    job.remainTime = job.runTime;
                    job.startTime = 0;
                    System.out.println(job.jobName + ", " + job.runTime + "milliseconds to excecute.");
                    waitQ.enqueue(job); //enqueue
                    job = null;
                    first = true;
                }

            }
            in.close(); //close the input stream
        }
        catch (Exception e) {
            System.err.println("Error: " + e.getMessage());
        }
        Job waitJob;
        System.out.println("\nBegin Job Processing");
        System.out.println("---------------------");
        boolean done = false;
        int elapsed = 0;
        while (!done) {
            while(!waitQ.isEmpty()) {
                waitJob = (Job)waitQ.dequeue();//dequeue
                runQ.enqueue(waitJob); //enqueue
            }

            Job runJob;
            while (!runQ.isEmpty()) {
                runJob = (Job)(runQ.dequeue());
                System.out.println("excecuting [" +runJob.jobName + "] " + runJob.remainTime + "milliseconds remaining");
                if (slice < runJob.remainTime) {
                    runJob.remainTime -= slice;
                    if (runQ.isEmpty() && waitQ.isEmpty())
                        elapsed += slice;
                    else
                        elapsed += (slice + timeSW);

                    waitQ.enqueue(runJob);
                }else {
                    int num = runJob.runTime + timeSW + runJob.remainTime;
                    System.out.println("*" + runJob.jobName + ", total time to execute was " +num+ "milliseconds");

                    }
                }
            }
    }
}

我的程序有效,但我的输出结果如下。我的问题是为什么它输出不正确?如果您能帮助我更正我的测试程序类以使输出匹配,那就太棒了。只是帮助我找到我的错误或我需要添加的东西谢谢!:) *

Begin Job Processing
---------------------
excecuting [Media Player] 100milliseconds remaining
excecuting [Angry Birds] 140milliseconds remaining
excecuting [Media Player] 50milliseconds remaining
*Media Player, total time to execute was 151milliseconds
excecuting [Angry Birds] 90milliseconds remaining
excecuting [Angry Birds] 40milliseconds remaining
*Angry Birds, total time to execute was 181milliseconds
4

1 回答 1

1

如果您在获得后查看代码,您会注意到您从未更新作业的运行时。从作业剩余时间中减去时间片后,您还应该更新作业已执行的时间。

“愤怒的小鸟”运行了 50 毫秒(当还剩 90 毫秒时),但是一旦您将该作业重新排入队列,您就会丢失该信息。

编辑:如果你基本上想让我给你答案。以下是您需要添加的缺失位。

runJob.runtime += slice 

(否则,您可以选择将每个作业的 elapsed 重新初始化为 0 并更改 `runJob.runtime += elapsed,因为 elapsed 已经包含延迟),然后再将作业排入队列。

于 2012-04-22T08:13:27.650 回答