0

我在尝试查看以下代码出错的地方时遇到了一个小问题:

public void processNextJob() {
        /*
         * 1. get # of free CPU's still avaialble
         * 2. get top most job from priority queue
         * 3. run job - put to CPU queue
         * 4. develop a CPU queue here
         * 5. count cores against freeCPUS and some sort of calculation to sort run times
         */
        int freeCPUS = 500;
        int availableCPUS = 0;
        JobRequest temp = new JobRequest(); // initalised to new JobRequest
        Queue q = new LinkedList();

        while (true) {
            int size = q.size();
            for (int i = 0; i < size; i++) {
                temp = (JobRequest) q.peek();
                if (temp != null) {
                    availableCPUS += temp.getCores();
                }
            }
            if ((freeCPUS - availableCPUS) >= 0) {
                JobRequest nextJob = schedulerPriorityQueue.closestDeadlineJob(freeCPUS - availableCPUS); // returns top job from queue
                if (nextJob != null) {
                    System.out.println("Top priority / edf job:");
                    printJob(nextJob);
                    q.add(nextJob);

                } else {
                    System.out.println("Job = null");
                }

            } else {
                break;
            }
        }
        if (temp != null) {

            System.out.println("Execution Queue");

            for(Object jr : q){
             printJob((JobRequest)jr);//print all elements in q   
            }

        }

    }

这里发生的事情是我从一个优先队列中添加顶部元素并将其添加到一个新的 LinkedList 中。但是我要取消优先队列的工作有一个名为“cores”的项目。我试图让它在保持在核心限制之下的同时尽可能多地裁员。

temp.getCores()是我获得核心价值的地方

我遇到的问题是它没有正确地将它们添加到我的链表队列中,它接收的值没有改变。我的队列显示 5 个输出,核心值为“160”,但我设置了 500 个上限,所以队列根本不满足

我看不出我在添加优先级队列中的值以使我的可用 CPUS 达到低于 500 的指定限制时哪里出错了。

编辑:

public JobRequest closestDeadlineJob(int freeCPUS) {
        // find top job to determine if other jobs for date need to be considered
        JobRequest nextJob = scheduledJobs.peek(); // return top most job

        if (nextJob != null) {

            System.out.println("Found top EDF job:");
            printJob( nextJob );

            // what is it's date?
            Date highestRankedDate = nextJob.getConvertedDeadlineDate();

            // create a temporary queue to work out priorities of jobs with same deadline
            JobPriorityQueue schedulerPriorityQueue = new JobPriorityQueue();

            // add the top job to priority queue
            //schedulerPriorityQueue.addJob(nextJob);

            for (JobRequest jr : scheduledJobs) {

                // go through scheduled jobs looking for all jobs with same date
                if (jr.getConvertedDeadlineDate().equals(highestRankedDate)) {
                    // same date deadline, soadd to scheduler priority queue
                    schedulerPriorityQueue.addJob(jr);
                    System.out.println("Adding following job to priority queue:");
                    printJob(jr);
                }
            }

            JobRequest highestPriorityJob = schedulerPriorityQueue.poll();
            // this is the item at the top of the PRIORTY JOB queue to return 

            // remove that item from scheduledJobs
            scheduledJobs.remove(highestPriorityJob);


            return highestPriorityJob;
        } else {
            return null;
        }
    }
4

1 回答 1

0

我想你需要看看这里。

在您for()增加变量的循环中availableCPUS,您总是head使用Queue:

temp = (JobRequest) q.peek();

而且由于peek()不会从队列中删除元素,因此您最终会将其分配JobRequesttemp.

尝试使用 anIterator而不是for

availableCPUS = 0;
Iterator<JobRequest> it = q.iterator();
while (it.hasNext()) {
    temp = it.next();
    if (temp != null) {
        availableCPUS += temp.getCores();
    }
}

另外,我认为您需要为 while 循环的每次迭代设置availableCPUS0才能正确计算内核数。

所以你应该把它放在availableCPUS = 0;Iterator 初始化的上面。

于 2013-02-25T12:00:14.783 回答