java - 如何在循环中检查所有任务的完成 while\for

标签 java for-loop while-loop

我有一个 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/

相关文章:

c++ - 在 'break' 中使用 'for-loop' 对性能的影响

java - String .replace() 不适用

java - 如何使用 hibernate 将输出列合并为一列?

java - 当使用带有 varargs 参数的构造函数时,如何强制至少必须传递一个参数?

php - 为什么 count 比 $count 差

C 代码 : Slowing down While loop after each iteration?

java - Android:正确更新 BaseAdapter 的 ArrayList?

c++ - 为什么我无法在循环中覆盖变量?

C++慢循环计算

sql - 我可以在 WHILE 循环中使用 CASE 语句吗?