我有一个 Routine 对象的 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;
}
}
}
不幸的是,在所有 Routine 完成之前,我的实现中的 boolean 值 allRoutineComplete
被设置为 true。
我的代码有什么问题?
如何高效地检查所有任务的完成情况?
最佳答案
您必须在第一个for
循环中移动所有Routine Complete 检查,否则该routine finished 循环更多次。
尝试使用这个:
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();
}
}
关于java - 如何在循环中检查所有任务的完成 while\for,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11174567/