2013-02-25 5 views
0

내가 다음 코드에서 틀린거야 어디 보려고 약간의 문제가 발생 해요 :자바 큐 calculaton에게 큐에 만족하지

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에 추가합니다. 하지만 우선 순위 대기열에서 제거 할 작업에는 "코어"라는 값이있는 항목이 있습니다. 나는 핵심 한도 밑에 머물면서 가능한 한 많은 일자리를 없애려고 노력하고있다. 내가 코어 값

를 얻을 곳

temp.getCores() 내가 데 문제는 내 LinkedList의 대기열이 나던 변화에 소요되는 값으로 올바르게 추가 아니라고이다. 내 대기열은 코어 값이 "160"인 5 개의 출력을 표시하지만 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; 
     } 
    } 
+0

이 명령문이 실행될 때 -'System.out.println ("Top priority/edf job :");'? –

+0

예 그것은 내 우선 순위 대기열에서 나의 최고 일을 알려준 다음 그 일을 연결된 목록에 추가합니다. 그것이 작동 하는지를 확인하기위한 것입니다. 모두 실행되지만 문제는 내가 코어를 추가하는 방법입니다. 작동하지 않는다 –

+0

'nearestDeadlineJob' 메서드가 무엇을하는지 알 수 있습니까? –

답변

0

난 당신이 여기에 모습을 가질 필요가 있다고 생각 : 내 사용 가능한 CPU가 500

편집 아래에있는 그것의 지정된 한계에 도달 할 수 PriorityQueue 인. 당신이 availableCPUS 변수를 증가하여 for() 루프에서

, 당신은 항상 headQueue의 복용 :

temp = (JobRequest) q.peek();

을 그리고 peek() 때문에 대기열에서 요소를 제거하지 않습니다, 당신은 할당 끝 동일 JobRequest ~ temp. Iterator 대신 for의를 사용하여

시도 : 또한

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

, 난 당신이 올바르게 코어를 계산 할 수 있도록 while 루프의 모든 반복에 대해 0availableCPUS를 설정할 필요가 있다고 생각합니다.

따라서 반복자 초기화 바로 위에 availableCPUS = 0;을 배치해야합니다.

+0

그래도 그 번호를 변경하면 : temp = (JobRequest) q.투표(); 이것은 맨 위를 제거 할 것이고, 내 q는 비어 있으며, 내 실행 스택을 출력 할 수 없다. –

+0

반복자를 사용한다면'poll'을 사용할 필요가 없다. –

+0

ah 우수, 이전에 넣은 것을 구현하여 사용 가능한 CPU가 내 (실제) 내에 있음을 확인하여 제한 세트를 충족했습니다. 매우 많이 –