Top Description Methods
com.google.common.util.concurrent

public Interface ListeningExecutorService

extends ExecutorService
Known Direct Subinterfaces
com.google.common.util.concurrent.ListeningScheduledExecutorService
Known Direct Implementers
com.google.common.util.concurrent.AbstractListeningExecutorService, com.google.common.util.concurrent.ForwardingListeningExecutorService
Annotations
@DoNotMock:Use TestingExecutors.sameThreadScheduledExecutor, or wrap a real Executor from java.util.concurrent.Executors with MoreExecutors.listeningDecorator
@GwtIncompatible
@ElementTypesAreNonnullByDefault
Static Imports
com.google.common.util.concurrent.Internal.toNanosSaturated

An ExecutorService that returns ListenableFuture instances. To create an instance from an existing ExecutorService, call MoreExecutors#listeningDecorator(ExecutorService).
Author
Chris Povirk
Since
10.0

Method Summary

Modifier and TypeMethod and Description
public default boolean
awaitTermination(Duration timeout)

Duration-based overload of awaitTermination(long, TimeUnit).

public <T extends @Nullable Object> List<Future<T>>

Returns:

A list of ListenableFuture instances representing the tasks, in the same sequential order as produced by the iterator for the given task list, each of which has completed.
invokeAll
(Collection<? extends Callable<T>>
the collection of tasks
tasks
)

Redeclares java.util.concurrent.ExecutorService.invokeAll.

Executes the given tasks, returning a list of Futures holding their status and results when all complete.
public <T extends @Nullable Object> List<Future<T>>

Returns:

a list of ListenableFuture instances representing the tasks, in the same sequential order as produced by the iterator for the given task list. If the operation did not time out, each task will have completed. If it did time out, some of these tasks will not have completed.
invokeAll
(Collection<? extends Callable<T>>
the collection of tasks
tasks
,
long
the maximum time to wait
timeout
,
TimeUnit
the time unit of the timeout argument
unit
)

Redeclares java.util.concurrent.ExecutorService.invokeAll.

Executes the given tasks, returning a list of Futures holding their status and results when all complete or the timeout expires, whichever happens first.
public default <T extends @Nullable Object> List<Future<T>>
invokeAll(Collection<? extends Callable<T>> tasks, Duration timeout)

Duration-based overload of invokeAll(Collection, long, TimeUnit).

public default <T extends @Nullable Object> T
invokeAny(Collection<? extends Callable<T>> tasks, Duration timeout)

Duration-based overload of invokeAny(Collection, long, TimeUnit).

public <T extends @Nullable Object> ListenableFuture<T>

Returns:

a ListenableFuture representing pending completion of the task
submit
(Callable<T>
the task to submit
task
)

Redeclares java.util.concurrent.ExecutorService.submit.

Submits a value-returning task for execution and returns a Future representing the pending results of the task.
public ListenableFuture<?>

Returns:

a ListenableFuture representing pending completion of the task
submit
(Runnable
the task to submit
task
)

Redeclares java.util.concurrent.ExecutorService.submit.

Submits a Runnable task for execution and returns a Future representing that task.
public <T extends @Nullable Object> ListenableFuture<T>

Returns:

a ListenableFuture representing pending completion of the task
submit
(Runnable
the task to submit
task
,
T
the result to return
result
)

Redeclares java.util.concurrent.ExecutorService.submit.

Submits a Runnable task for execution and returns a Future representing that task.
Inherited from java.util.concurrent.ExecutorService:
awaitTerminationcloseinvokeAnyinvokeAnyisShutdownisTerminatedshutdownshutdownNow

Method Detail

awaitTerminationback to summary
public default boolean awaitTermination(Duration timeout) throws InterruptedException

Duration-based overload of awaitTermination(long, TimeUnit).

Annotations
@J2ktIncompatible
Since
32.1.0
invokeAllback to summary
public <T extends @Nullable Object> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) throws InterruptedException

Redeclares java.util.concurrent.ExecutorService.invokeAll.

Doc from java.util.concurrent.ExecutorService.invokeAll.

Executes the given tasks, returning a list of Futures holding their status and results when all complete. Future#isDone is true for each element of the returned list. Note that a completed task could have terminated either normally or by throwing an exception. The results of this method are undefined if the given collection is modified while this operation is in progress.

All elements in the returned list must be ListenableFuture instances. The easiest way to obtain a List<ListenableFuture<T>> from this method is an unchecked (but safe) cast:

  @SuppressWarnings("unchecked") // guaranteed by invokeAll contract
  List<ListenableFuture<T>> futures = (List) executor.invokeAll(tasks);
Parameters
<T>
the type of the values returned from the tasks
tasks:Collection<? extends Callable<T>>

the collection of tasks

Returns:List<Future<T>>

A list of ListenableFuture instances representing the tasks, in the same sequential order as produced by the iterator for the given task list, each of which has completed.

Annotations
@Override
Exceptions
InterruptedException:
if interrupted while waiting, in which case unfinished tasks are cancelled
RejectedExecutionException:
if any task cannot be scheduled for execution
NullPointerException:
if any task is null
invokeAllback to summary
public <T extends @Nullable Object> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException

Redeclares java.util.concurrent.ExecutorService.invokeAll.

Doc from java.util.concurrent.ExecutorService.invokeAll.

Executes the given tasks, returning a list of Futures holding their status and results when all complete or the timeout expires, whichever happens first. Future#isDone is true for each element of the returned list. Upon return, tasks that have not completed are cancelled. Note that a completed task could have terminated either normally or by throwing an exception. The results of this method are undefined if the given collection is modified while this operation is in progress.

All elements in the returned list must be ListenableFuture instances. The easiest way to obtain a List<ListenableFuture<T>> from this method is an unchecked (but safe) cast:

  @SuppressWarnings("unchecked") // guaranteed by invokeAll contract
  List<ListenableFuture<T>> futures = (List) executor.invokeAll(tasks, timeout, unit);
Parameters
<T>
the type of the values returned from the tasks
tasks:Collection<? extends Callable<T>>

the collection of tasks

timeout:long

the maximum time to wait

unit:TimeUnit

the time unit of the timeout argument

Returns:List<Future<T>>

a list of ListenableFuture instances representing the tasks, in the same sequential order as produced by the iterator for the given task list. If the operation did not time out, each task will have completed. If it did time out, some of these tasks will not have completed.

Annotations
@Override
Exceptions
InterruptedException:
if interrupted while waiting, in which case unfinished tasks are cancelled
RejectedExecutionException:
if any task cannot be scheduled for execution
NullPointerException:
if any task is null
invokeAllback to summary
public default <T extends @Nullable Object> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, Duration timeout) throws InterruptedException

Duration-based overload of invokeAll(Collection, long, TimeUnit).

Annotations
@J2ktIncompatible
Since
32.1.0
invokeAnyback to summary
public default <T extends @Nullable Object> T invokeAny(Collection<? extends Callable<T>> tasks, Duration timeout) throws InterruptedException, ExecutionException, TimeoutException

Duration-based overload of invokeAny(Collection, long, TimeUnit).

Annotations
@J2ktIncompatible
Since
32.1.0
submitback to summary
public <T extends @Nullable Object> ListenableFuture<T> submit(Callable<T> task)

Redeclares java.util.concurrent.ExecutorService.submit.

Doc from java.util.concurrent.ExecutorService.submit.

Submits a value-returning task for execution and returns a Future representing the pending results of the task. The Future's get method will return the task's result upon successful completion.

If you would like to immediately block waiting for a task, you can use constructions of the form result = exec.submit(aCallable).get();

Note

The Executors class includes a set of methods that can convert some other common closure-like objects, for example, java.security.PrivilegedAction to Callable form so they can be submitted.

Parameters
<T>
the type of the task's result
task:Callable<T>

the task to submit

Returns:ListenableFuture<T>

a ListenableFuture representing pending completion of the task

Annotations
@Override
Exceptions
RejectedExecutionException:
if the task cannot be scheduled for execution
submitback to summary
public ListenableFuture<?> submit(Runnable task)

Redeclares java.util.concurrent.ExecutorService.submit.

Doc from java.util.concurrent.ExecutorService.submit.

Submits a Runnable task for execution and returns a Future representing that task. The Future's get method will return null upon successful completion.

Parameters
task:Runnable

the task to submit

Returns:ListenableFuture<?>

a ListenableFuture representing pending completion of the task

Annotations
@Override
Exceptions
RejectedExecutionException:
if the task cannot be scheduled for execution
submitback to summary
public <T extends @Nullable Object> ListenableFuture<T> submit(Runnable task, T result)

Redeclares java.util.concurrent.ExecutorService.submit.

Doc from java.util.concurrent.ExecutorService.submit.

Submits a Runnable task for execution and returns a Future representing that task. The Future's get method will return the given result upon successful completion.

Parameters
<T>
the type of the result
task:Runnable

the task to submit

result:T

the result to return

Returns:ListenableFuture<T>

a ListenableFuture representing pending completion of the task

Annotations
@Override
Exceptions
RejectedExecutionException:
if the task cannot be scheduled for execution