我想创建一个运行队列中所有作业的方法(作业列表)。我创建了一个运行一个作业的方法(runAJob
),而下面的方法是运行队列列表中的所有作业,直到时间(myTimeLeft)用完为止。
现在,当我运行runAll
方法时,此代码总是会留下1个ArraLyist。有人会告诉我为什么这是错的吗?
/**
* 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: ");
}
}
答案 0 :(得分:0)
为什么你(大概)使用队列时有for循环?您不会根据队列的大小(i
)做出任何决定。我会坚持让Queue处理自己:
// declarations and definitions
while (myJobDuration < myTimeLeft && !myJobInQueue.isEmpty()) {
// get next item from queue
runAJob();
}