boolean () { synchronized (lock) { // other thread produced something or all of them reached the end of list boolean producedSomething = subClassIterator.hasNext(); hasNext.set(producedSomething); // store the result to avoid locking again after exit return producedSomething || !candidatesToFindSubclassesIterator.hasNext() || classesBeingProcessed.isEmpty(); } }