Source:
private void startLookups(List<LookupBean> lookupBeanList) { ImmutableMap.Builder<String, LookupExtractorFactoryContainer> builder = ImmutableMap.builder(); ListeningScheduledExecutorService executorService = MoreExecutors.listeningDecorator( Executors.newScheduledThreadPool( lookupConfig.getNumLookupLoadingThreads(), Execs.makeThreadFactory("LookupReferencesManager-Startup-%s") ) ); try { List<ListenableFuture<Map.Entry>> futures = new ArrayList<>(); LOG.info("Starting lookup loading process"); for (LookupBean lookupBean : lookupBeanList) { futures.add( executorService.submit( () -> { LookupExtractorFactoryContainer container = lookupBean.getContainer(); LOG.info( "Starting lookup [%s]:[%s]", lookupBean.getName(), container ); if (container.getLookupExtractorFactory().start()) { LOG.info( "Started lookup [%s]:[%s]", lookupBean.getName(), container ); return new AbstractMap.SimpleImmutableEntry<>(lookupBean.getName(), container); } else { LOG.error( "Failed to start lookup [%s]:[%s]", lookupBean.getName(), container ); return null; } } ) ); } final ListenableFuture<List<Map.Entry>> futureList = Futures.allAsList(futures); try { futureList.get() .stream() .filter(Objects::nonNull) .forEach(builder::put); stateRef.set(new LookupUpdateState(builder.build(), ImmutableList.of(), ImmutableList.of())); } catch (InterruptedException | ExecutionException ex) { futureList.cancel(true); throw ex; } } catch (Exception e) { LOG.error(e, "Failed to finish lookup load process."); } finally { executorService.shutdownNow(); } }

Target:
private void startLookups(List<LookupBean> lookupBeanList) { ImmutableMap.Builder<String, LookupExtractorFactoryContainer> builder = ImmutableMap.builder(); ExecutorService executorService = Execs.multiThreaded( lookupConfig.getNumLookupLoadingThreads(), "LookupReferencesManager-Startup-%s" ); ExecutorCompletionService completionService = new ExecutorCompletionService(executorService); List<Future<Map.Entry>> futures = new ArrayList<>(); try { LOG.info("Starting lookup loading process"); for (LookupBean lookupBean : lookupBeanList) { futures.add( completionService.submit( () -> { LookupExtractorFactoryContainer container = lookupBean.getContainer(); LOG.info( "Starting lookup [%s]:[%s]", lookupBean.getName(), container ); if (container.getLookupExtractorFactory().start()) { LOG.info( "Started lookup [%s]:[%s]", lookupBean.getName(), container ); return new AbstractMap.SimpleImmutableEntry<>(lookupBean.getName(), container); } else { LOG.error( "Failed to start lookup [%s]:[%s]", lookupBean.getName(), container ); return null; } } ) ); } for (int i = 0; i < futures.size(); i++) { try { final Future<AbstractMap.SimpleImmutableEntry<String, LookupExtractorFactoryContainer>> completedFuture = completionService .take(); final AbstractMap.SimpleImmutableEntry<String, LookupExtractorFactoryContainer> lookupResult = completedFuture .get(); if (lookupResult != null) { builder.put(lookupResult); } } catch (ExecutionException e) { LOG.error(e, "Execution error during lookup loading."); } } stateRef.set(new LookupUpdateState(builder.build(), ImmutableList.of(), ImmutableList.of())); } catch (Exception e) { LOG.error(e, "Failed to finish lookup load process."); for (Future future : futures) { future.cancel(true); } } finally { executorService.shutdownNow(); } }