我有一个例程对象的ArrayList,我必须使用这个方法来更新任务的各种状态
while(!allRoutineComplete){
for (Routine routine : routineList) {
if(!(routine.isBlocked()) && !(routine.isFinished())) {
routine.run();
}
}
for (Routine routine : routineList) {
routine.updateStatus();
if(routine.isFinished()){
completedRoutineNumber++;
}
if(completedRoutineNumber==routineList.size()){
allRoutineComplete=true;
}
}
}不幸的是,在所有例程结束之前,my实现中的布尔值allRoutineComplete被设置为true。
我的代码出了什么问题?
如何有效地检查所有任务的完成情况?
发布于 2012-06-24 19:22:41
您必须在第一个for循环中移动所有例程完成检查,否则例程结束循环的次数更多。
尝试使用以下命令:
while(!allRoutineComplete){
for (Routine routine : routineList) {
if(!(routine.isBlocked()) && !(routine.isFinished())) {
routine.run();
if(routine.isFinished()){
completedRoutineNumber++;
}
if(completedRoutineNumber==routineList.size()){
allRoutineComplete=true;
}
}
}
for (Routine routine : routineList) {
routine.updateStatus();
}
}发布于 2012-06-24 10:44:26
我怀疑问题在于您没有在while循环的顶部设置completedRoutineNumber =0。因为您要迭代整个例程列表,所以如果一半的例程都完成了,而while循环运行了两次以上,那么即使只有一半的例程完成,completedRoutineNumber也会第三次超过routineList.size()。
https://stackoverflow.com/questions/11174567
复制相似问题