MCPcopy Create free account
hub / github.com/antlr/codebuff / invokeAnyImpl

Method invokeAnyImpl

output/java_guava/1.4.16/MoreExecutors.java:643–711  ·  view source on GitHub ↗

An implementation of ExecutorService#invokeAny for ListeningExecutorService implementations.

(
    ListeningExecutorService executorService,
    Collection<? extends Callable<T>> tasks,
    boolean timed,
    long nanos)

Source from the content-addressed store, hash-verified

641 */
642
643 @GwtIncompatible
644 static <T> T invokeAnyImpl(
645 ListeningExecutorService executorService,
646 Collection<? extends Callable<T>> tasks,
647 boolean timed,
648 long nanos) throws InterruptedException, ExecutionException, TimeoutException {
649 checkNotNull(executorService);
650 int ntasks = tasks.size();
651 checkArgument(ntasks > 0);
652 List<Future<T>> futures = Lists.newArrayListWithCapacity(ntasks);
653 BlockingQueue<Future<T>> futureQueue = Queues.newLinkedBlockingQueue();
654
655 // For efficiency, especially in executors with limited
656 // parallelism, check to see if previously submitted tasks are
657 // done before submitting more of them. This interleaving
658 // plus the exception mechanics account for messiness of main
659 // loop.
660 try {
661 // Record exceptions so that if we fail to obtain any
662 // result, we can throw the last exception we got.
663 ExecutionException ee = null;
664 long lastTime = timed ? System.nanoTime() : 0;
665 Iterator<? extends Callable<T>> it = tasks.iterator();
666 futures.add(submitAndAddQueueListener(executorService, it.next(), futureQueue));
667 --ntasks;
668 int active = 1;
669 while (true) {
670 Future<T> f = futureQueue.poll();
671 if (f == null) {
672 if (ntasks > 0) {
673 --ntasks;
674 futures.add(submitAndAddQueueListener(executorService, it.next(), futureQueue));
675 ++active;
676 } else if (active == 0) {
677 break;
678 } else if (timed) {
679 f = futureQueue.poll(nanos, TimeUnit.NANOSECONDS);
680 if (f == null) {
681 throw new TimeoutException();
682 }
683
684 long now = System.nanoTime();
685 nanos -= now - lastTime;
686 lastTime = now;
687 } else {
688 f = futureQueue.take();
689 }
690 }
691 if (f != null) {
692 --active;
693 try {
694 return f.get();
695 } catch (ExecutionException eex) {
696 ee = eex;
697 } catch (RuntimeException rex) {
698 ee = new ExecutionException(rex);
699 }
700 }

Callers

nothing calls this directly

Calls 13

sizeMethod · 0.65
iteratorMethod · 0.65
addMethod · 0.65
nextMethod · 0.65
getMethod · 0.65
checkNotNullMethod · 0.45
checkArgumentMethod · 0.45
pollMethod · 0.45
takeMethod · 0.45

Tested by

no test coverage detected