We have inherited some code that is doing serial processing of a Queue of futures. I'd like to speed it up by running the processing in parallel, but I'm having trouble figuring out how to properly increment the success / failure counts that we need to track while processing.
This is what existed before:
private void flushFutures(final Queue<Future<State>> futures) {
final Map<String, Integer> someMapA = new HashMap<>();
while(!futures.isEmpty()) {
final Future<State> future = futures.poll();
final State state = future.get();
switch (state) {
case SOME_CASE_A:
// modify someMapA
break;
case SOME_CASE_B:
// modify someMapB
break;
//.....
}
}
}
So I tried replacing the while loop with a:
futures.parallelStream().forEach(future -> ...)
But that leaves me with problems altering state from within a lambda.
Does anyone have tips on how I could better handle these futures to do the same thing as its currently doing, but sequentially, and without the messy adding of the future back into the collection?