0

キュー (ジョブ リスト) 内のすべてのジョブを実行するメソッドを作成したいと考えています。1 つのジョブ ( runAJob) を実行するメソッドを作成しました。以下のメソッドは、時間 (myTimeLeft) がなくなるまで、キュー リスト内のすべてのジョブを実行することです。
現在、メソッドを実行すると、このコードは常に 1 つの ArraList を残します runAll。なぜそれが間違っているのか誰か教えてくれませんか?

   /**
  * Run the first job on the queue if there is enough time on the clock and the job queue list is not empty.
  * And move the job to the finished jobs list.
  */
public void runAJob(){
   myJobDuration = myJobInQueue.get(0).getDuration();
   if(!myJobInQueue.isEmpty())
   {
        if (myJobDuration < myTimeLeft)
        {
            myTimeLeft = myTimeLeft - myJobDuration;
            myFinishedJobs.add(myJobInQueue.get(0));
            System.out.println("A job is running: " + myJobInQueue.get(0).getName());
            myJobInQueue.remove(0);
        }
        else 
        {
            System.out.println("Not enogth running time left, please add time on the clock.");             
        }
   }
   else 
   {
     System.out.println("No pending job on the list.");
   }
}

/**
  * Run all the jobs on the queue in order until it runs out of time.
  */  
public void runAll()
{
  int counAJob =0; 
  while (myJobDuration < myTimeLeft && !myJobInQueue.isEmpty()) {
   // get next item from queue
     runAJob();
     System.out.println("A job is running: ");
  }

}
4

1 に答える 1

0

(おそらく)キューを使用しているのに、なぜforループがあるのですか?キューのサイズ()に基づいて決定を行うことはありませんi。私はキューにそれ自体を処理させることに固執するでしょう:

// declarations and definitions

while (myJobDuration < myTimeLeft && !myJobInQueue.isEmpty()) {
    // get next item from queue
    runAJob();
}
于 2012-10-13T15:48:09.593 に答える