Reputation: 388
I have an ArrayList of Routine objects and I must use this method to update the various status of the task
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;
}
}
}
unfortunately the boolean allRoutineComplete
in the my implementation is setted as true before the finish of all the Routine.
What is wrong in my code?
How to check efficently the finish of all tasks?
Upvotes: 1
Views: 109
Reputation: 15845
You must move the all Routine Complete check, in the first for
cycle, otherwise the routine finished loop more times.
Try to use this:
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();
}
}
Upvotes: 2
Reputation: 3773
I suspect the problem is that you are not setting completedRoutineNumber = 0 at the top of the while loop. Since you are iterating the whole list of routines, if half the routines are complete and the while loop runs more than twice, completedRoutineNumber will exceed routineList.size() the 3rd time through even though only half the routines are complete.
Upvotes: 1