Source:
<code>public Map<EventTypePartition, Duration> getTimeLags(final Collection<NakadiCursor> committedPositions, final List<PartitionEndStatistics> endPositions) throws ErrorGettingCursorTimeLagException, InconsistentStateException { <START> final ExecutorService executor = Executors.newFixedThreadPool(LAG_CALCULATION_PARALLELISM); <END> final Map<EventTypePartition, Future<Duration>> futures = new HashMap<>(); final Map<EventTypePartition, Duration> result = new HashMap<>(); for (final NakadiCursor cursor : committedPositions) { final EventTypePartition partition = new EventTypePartition(cursor.getTimeline().getEventType(), cursor.getPartition()); if (isCursorAtTail(cursor, endPositions)) { result.put(partition, Duration.ZERO); } else { futures.put(partition, executor.submit(() -> getNextEventTimeLag(cursor))); } } executor.shutdown(); try { final boolean finished = executor.awaitTermination(COMPLETE_TIMEOUT_MS, TimeUnit.MILLISECONDS); if (!finished) { throw new InconsistentStateException("Timeout occurred when getting subscription time lag"); } for (final EventTypePartition partition : futures.keySet()) { final Future<Duration> future = futures.get(partition); try { result.put(partition, future.get()); } catch (final ExecutionException e) { throw e.getCause(); } } return result; } catch (final InconsistentStateException | ErrorGettingCursorTimeLagException e) { throw e; } catch (final Throwable e) { throw new InconsistentStateException("Unexpected error occurred when getting subscription time lag", e); } }</code><technical_language>sense constantly create threads in packs of 10? single CachedThreadPool for this service</technical_language>

Target:
public Map<EventTypePartition, Duration> getTimeLags(final Collection<NakadiCursor> committedPositions, final List<PartitionEndStatistics> endPositions) throws ErrorGettingCursorTimeLagException, InconsistentStateException, LimitReachedException, TimeLagStatsTimeoutException { final TimeLagRequestHandler timeLagHandler = new TimeLagRequestHandler(timelineService, threadPool); final Map<EventTypePartition, Duration> timeLags = new HashMap<>(); final Map<EventTypePartition, CompletableFuture<Duration>> futureTimeLags = new HashMap<>(); try { for (final NakadiCursor cursor : committedPositions) { if (isCursorAtTail(cursor, endPositions)) { timeLags.put(cursor.getEventTypePartition(), Duration.ZERO); } else { final CompletableFuture<Duration> timeLagFuture = timeLagHandler.getCursorTimeLagFuture(cursor); futureTimeLags.put(cursor.getEventTypePartition(), timeLagFuture); } } CompletableFuture .allOf(futureTimeLags.values().toArray(new CompletableFuture[futureTimeLags.size()])) .get(timeLagHandler.getRemainingTimeoutMs(), TimeUnit.MILLISECONDS); for (final EventTypePartition partition : futureTimeLags.keySet()) { timeLags.put(partition, futureTimeLags.get(partition).get()); } return timeLags; } catch (final RejectedExecutionException e) { throw new LimitReachedException("Time lag statistics thread pool exhausted", e); } catch (final TimeoutException e) { throw new TimeLagStatsTimeoutException("Timeout exceeded for time lag statistics", e); } catch (final ExecutionException e) { if (e.getCause() instanceof MyNakadiRuntimeException1) { throw (MyNakadiRuntimeException1) e.getCause(); } else { throw new InconsistentStateException("Unexpected error occurred when getting subscription time lag", e.getCause()); } } catch (final Throwable e) { throw new InconsistentStateException("Unexpected error occurred when getting subscription time lag", e); } }