@PrivateApi public static void waitForever(Collection<? extends Future> futuresToWaitFor, ExceptionHandler exceptionHandler) { Collection<Future> futures = new ArrayList<Future>(futuresToWaitFor); while (true) { Iterator<Future> it = futures.iterator(); while (it.hasNext()) { Future future = it.next(); try { future.get(); } catch (Exception e) { exceptionHandler.handleException(e); } if (future.isDone() || future.isCancelled()) { it.remove(); } } if (futures.isEmpty()) { return; } } }
@PrivateApi public static <V> Collection<V> returnWithDeadline(Collection<Future<V>> futures, long overallTimeout, TimeUnit overallTimeUnit, long perFutureTimeout, TimeUnit perFutureTimeUnit, ExceptionHandler exceptionHandler) { // Calculate timeouts for whole operation and per future. If corresponding TimeUnits not set assume // the default of TimeUnit.SECONDS long overallTimeoutNanos = calculateTimeout(overallTimeout, overallTimeUnit); long perFutureTimeoutNanos = calculateTimeout(perFutureTimeout, perFutureTimeUnit); // Common deadline for all futures long deadline = System.nanoTime() + overallTimeoutNanos; List<V> results = new ArrayList<V>(futures.size()); for (Future<V> future : futures) { try { long timeoutNanos = calculateFutureTimeout(perFutureTimeoutNanos, deadline); V value = executeWithDeadline(future, timeoutNanos); if (value != null) { results.add(value); } } catch (Exception e) { exceptionHandler.handleException(e); } } return results; }
@PrivateApi public static <V> Collection<V> returnWithDeadline(Collection<Future<V>> futures, long overallTimeout, TimeUnit overallTimeUnit, long perFutureTimeout, TimeUnit perFutureTimeUnit, ExceptionHandler exceptionHandler) { // Calculate timeouts for whole operation and per future. If corresponding TimeUnits not set assume // the default of TimeUnit.SECONDS long overallTimeoutNanos = calculateTimeout(overallTimeout, overallTimeUnit); long perFutureTimeoutNanos = calculateTimeout(perFutureTimeout, perFutureTimeUnit); // Common deadline for all futures long deadline = System.nanoTime() + overallTimeoutNanos; List<V> results = new ArrayList<V>(futures.size()); for (Future<V> future : futures) { try { long timeoutNanos = calculateFutureTimeout(perFutureTimeoutNanos, deadline); V value = executeWithDeadline(future, timeoutNanos); if (value != null) { results.add(value); } } catch (Exception e) { exceptionHandler.handleException(e); } } return results; }
@PrivateApi public static void waitForever(Collection<? extends Future> futuresToWaitFor, ExceptionHandler exceptionHandler) { Collection<Future> futures = new ArrayList<Future>(futuresToWaitFor); while (true) { Iterator<Future> it = futures.iterator(); while (it.hasNext()) { Future future = it.next(); try { future.get(); } catch (Exception e) { exceptionHandler.handleException(e); } if (future.isDone() || future.isCancelled()) { it.remove(); } } if (futures.isEmpty()) { return; } } }
@PrivateApi public static void waitWithDeadline(Collection<? extends Future> futures, long overallTimeout, TimeUnit overallTimeUnit, long perFutureTimeout, TimeUnit perFutureTimeUnit, ExceptionHandler exceptionHandler) { // Calculate timeouts for whole operation and per future. If corresponding TimeUnits not set assume // the default of TimeUnit.SECONDS long overallTimeoutNanos = calculateTimeout(overallTimeout, overallTimeUnit); long perFutureTimeoutNanos = calculateTimeout(perFutureTimeout, perFutureTimeUnit); // Common deadline for all futures long deadline = System.nanoTime() + overallTimeoutNanos; for (Future future : futures) { try { long timeoutNanos = calculateFutureTimeout(perFutureTimeoutNanos, deadline); executeWithDeadline(future, timeoutNanos); } catch (Throwable e) { exceptionHandler.handleException(e); } } }
@PrivateApi public static void waitWithDeadline(Collection<? extends Future> futures, long overallTimeout, TimeUnit overallTimeUnit, long perFutureTimeout, TimeUnit perFutureTimeUnit, ExceptionHandler exceptionHandler) { // Calculate timeouts for whole operation and per future. If corresponding TimeUnits not set assume // the default of TimeUnit.SECONDS long overallTimeoutNanos = calculateTimeout(overallTimeout, overallTimeUnit); long perFutureTimeoutNanos = calculateTimeout(perFutureTimeout, perFutureTimeUnit); // Common deadline for all futures long deadline = System.nanoTime() + overallTimeoutNanos; for (Future future : futures) { try { long timeoutNanos = calculateFutureTimeout(perFutureTimeoutNanos, deadline); executeWithDeadline(future, timeoutNanos); } catch (Throwable e) { exceptionHandler.handleException(e); } } }
@PrivateApi public static void waitUntilAllRespondedWithDeadline(Collection<? extends Future> futures, long timeout, TimeUnit timeUnit, ExceptionHandler exceptionHandler) { CollectAllExceptionHandler collector = new CollectAllExceptionHandler(futures.size()); waitWithDeadline(futures, timeout, timeUnit, collector); final List<Throwable> throwables = collector.getThrowables(); // synchronized list does not provide thread-safety guarantee for iteration so we handle it ourselves. synchronized (throwables) { for (Throwable t : throwables) { exceptionHandler.handleException(t); } } }
@PrivateApi public static void waitUntilAllRespondedWithDeadline(Collection<? extends Future> futures, long timeout, TimeUnit timeUnit, ExceptionHandler exceptionHandler) { CollectAllExceptionHandler collector = new CollectAllExceptionHandler(futures.size()); waitWithDeadline(futures, timeout, timeUnit, collector); final List<Throwable> throwables = collector.getThrowables(); // synchronized list does not provide thread-safety guarantee for iteration so we handle it ourselves. synchronized (throwables) { for (Throwable t : throwables) { exceptionHandler.handleException(t); } } }