1

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?

CustardBun
  • 3,457
  • 8
  • 39
  • 65
  • 1
    It is unclear what you expect to gain by parallelizing that. Is the actual implementation of future not calculating the result until GET is called? – Affe Feb 26 '19 at 22:16

1 Answers1

2

Rather than using a forEach, you could do a map on futures.get(), then do a "groupBy" collector using a concurrent hash map.

See: Java 8 Parallel Stream Concurrent Grouping

aglassman
  • 2,643
  • 1
  • 17
  • 30