Interface ListeningExecutorService
-
- All Superinterfaces:
Executor
,ExecutorService
- All Known Subinterfaces:
ListeningScheduledExecutorService
- All Known Implementing Classes:
AbstractListeningExecutorService
,ForwardingListeningExecutorService
@DoNotMock("Use TestingExecutors.sameThreadScheduledExecutor, or wrap a real Executor from java.util.concurrent.Executors with MoreExecutors.listeningDecorator") @GwtIncompatible public interface ListeningExecutorService extends ExecutorService
AnExecutorService
that returnsListenableFuture
instances. To create an instance from an existingExecutorService
, callMoreExecutors.listeningDecorator(ExecutorService)
.- Since:
- 10.0
- Author:
- Chris Povirk
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description <T> List<Future<T>>
invokeAll(Collection<? extends Callable<T>> tasks)
Executes the given tasks, returning a list of Futures holding their status and results when all complete.<T> List<Future<T>>
invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit)
Executes the given tasks, returning a list of Futures holding their status and results when all complete or the timeout expires, whichever happens first.ListenableFuture<?>
submit(Runnable task)
Submits a Runnable task for execution and returns a Future representing that task.<T> ListenableFuture<T>
submit(Runnable task, T result)
Submits a Runnable task for execution and returns a Future representing that task.<T> ListenableFuture<T>
submit(Callable<T> task)
Submits a value-returning task for execution and returns a Future representing the pending results of the task.-
Methods inherited from interface java.util.concurrent.ExecutorService
awaitTermination, invokeAny, invokeAny, isShutdown, isTerminated, shutdown, shutdownNow
-
-
-
-
Method Detail
-
submit
<T> ListenableFuture<T> submit(Callable<T> task)
Description copied from interface:java.util.concurrent.ExecutorService
Submits a value-returning task for execution and returns a Future representing the pending results of the task. The Future'sget
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,PrivilegedAction
toCallable
form so they can be submitted.- Specified by:
submit
in interfaceExecutorService
- Type Parameters:
T
- the type of the task's result- Parameters:
task
- the task to submit- Returns:
- a
ListenableFuture
representing pending completion of the task - Throws:
RejectedExecutionException
- if the task cannot be scheduled for execution
-
submit
ListenableFuture<?> submit(Runnable task)
Description copied from interface:java.util.concurrent.ExecutorService
Submits a Runnable task for execution and returns a Future representing that task. The Future'sget
method will returnnull
upon successful completion.- Specified by:
submit
in interfaceExecutorService
- Parameters:
task
- the task to submit- Returns:
- a
ListenableFuture
representing pending completion of the task - Throws:
RejectedExecutionException
- if the task cannot be scheduled for execution
-
submit
<T> ListenableFuture<T> submit(Runnable task, T result)
Description copied from interface:java.util.concurrent.ExecutorService
Submits a Runnable task for execution and returns a Future representing that task. The Future'sget
method will return the given result upon successful completion.- Specified by:
submit
in interfaceExecutorService
- Type Parameters:
T
- the type of the result- Parameters:
task
- the task to submitresult
- the result to return- Returns:
- a
ListenableFuture
representing pending completion of the task - Throws:
RejectedExecutionException
- if the task cannot be scheduled for execution
-
invokeAll
<T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) throws InterruptedException
Executes the given tasks, returning a list of Futures holding their status and results when all complete.Future.isDone()
istrue
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 aList<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);
- Specified by:
invokeAll
in interfaceExecutorService
- Type Parameters:
T
- the type of the values returned from the tasks- Parameters:
tasks
- the collection of tasks- 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. - Throws:
RejectedExecutionException
- if any task cannot be scheduled for executionNullPointerException
- if any task is nullInterruptedException
- if interrupted while waiting, in which case unfinished tasks are cancelled
-
invokeAll
<T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException
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()
istrue
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 aList<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);
- Specified by:
invokeAll
in interfaceExecutorService
- Type Parameters:
T
- the type of the values returned from the tasks- Parameters:
tasks
- the collection of taskstimeout
- the maximum time to waitunit
- the time unit of the timeout argument- 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. - Throws:
RejectedExecutionException
- if any task cannot be scheduled for executionNullPointerException
- if any task is nullInterruptedException
- if interrupted while waiting, in which case unfinished tasks are cancelled
-
-