0

我想创建一个运行队列中所有作业的方法(作业列表)。我创建了一个方法来运行一个作业(runAJob),下面的方法是运行队列列表中的所有作业,直到时间(myTimeLeft)用完。现在,当我运行方法
时,这段代码总是留下 1 个 ArraLyist 。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 回答