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

public final Class Futures

extends GwtFuturesCatchingSpecialization
Class Inheritance
Annotations
@GwtCompatible
emulated:true
@ElementTypesAreNonnullByDefault
Static Imports
com.google.common.base.Preconditions.checkNotNull, .Preconditions.checkState, com.google.common.util.concurrent.Internal.toNanosSaturated, .MoreExecutors.directExecutor, .Uninterruptibles.getUninterruptibly, java.util.Objects.requireNonNull

Static utility methods pertaining to the Future interface.

Many of these methods use the ListenableFuture API; consult the Guava User Guide article on ListenableFuture.

The main purpose of ListenableFuture is to help you chain together a graph of asynchronous operations. You can chain them together manually with calls to methods like Futures.transform, but you will often find it easier to use a framework. Frameworks automate the process, often adding features like monitoring, debugging, and cancellation. Examples of frameworks include:

If you do chain your operations manually, you may want to use FluentFuture.

Authors
Kevin Bourrillion, Nishant Thakkar, Sven Mawson
Since
1.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
public static class
Futures.FutureCombiner<V extends @Nullable Object>

A helper to create a new ListenableFuture whose result is generated from a combination of input futures.

private static class
private static class
private static class
Futures.NonCancellationPropagatingFuture<V extends @Nullable Object>

A wrapped future that does not propagate cancellation to its delegate.

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static <V extends @Nullable Object> void
addCallback(final ListenableFuture<V>
The future attach the callback to.
future
,
final FutureCallback<? super V>
The callback to invoke when future is completed.
callback
,
Executor
The executor to run callback when the future completes.
executor
)

Registers separate success and failure callbacks to be run when the Future's computation is complete or, if the computation is already complete, immediately.

public static <V extends @Nullable Object> ListenableFuture<List<V>>

Returns:

a future that provides a list of the results of the component futures
allAsList
(ListenableFuture<? extends V>...
futures to combine
futures
)

Creates a new ListenableFuture whose value is a list containing the values of all its input futures, if all succeed.

public static <V extends @Nullable Object> ListenableFuture<List<V>>

Returns:

a future that provides a list of the results of the component futures
allAsList
(Iterable<? extends ListenableFuture<? extends V>>
futures to combine
futures
)

Creates a new ListenableFuture whose value is a list containing the values of all its input futures, if all succeed.

public static <V extends @Nullable Object, X extends Throwable> ListenableFuture<V>
catching(ListenableFuture<? extends V>
the primary input Future
input
,
Class<X>
the exception type that triggers use of fallback. The exception type is matched against the input's exception. "The input's exception" means the cause of the ExecutionException thrown by input.get() or, if get() throws a different kind of exception, that exception itself. To avoid hiding bugs and other unrecoverable errors, callers should prefer more specific types, avoiding Throwable.class in particular.
exceptionType
,
Function<? super X, ? extends V>
the Function to be called if input fails with the expected exception type. The function's argument is the input's exception. "The input's exception" means the cause of the ExecutionException thrown by input.get() or, if get() throws a different kind of exception, that exception itself.
fallback
,
Executor
the executor that runs fallback if input fails
executor
)

Returns a Future whose result is taken from the given primary input or, if the primary input fails with the given exceptionType, from the result provided by the fallback.

public static <V extends @Nullable Object, X extends Throwable> ListenableFuture<V>
catchingAsync(ListenableFuture<? extends V>
the primary input Future
input
,
Class<X>
the exception type that triggers use of fallback. The exception type is matched against the input's exception. "The input's exception" means the cause of the ExecutionException thrown by input.get() or, if get() throws a different kind of exception, that exception itself. To avoid hiding bugs and other unrecoverable errors, callers should prefer more specific types, avoiding Throwable.class in particular.
exceptionType
,
AsyncFunction<? super X, ? extends V>
the AsyncFunction to be called if input fails with the expected exception type. The function's argument is the input's exception. "The input's exception" means the cause of the ExecutionException thrown by input.get() or, if get() throws a different kind of exception, that exception itself.
fallback
,
Executor
the executor that runs fallback if input fails
executor
)

Returns a Future whose result is taken from the given primary input or, if the primary input fails with the given exceptionType, from the result provided by the fallback.

public static <V extends @Nullable Object, X extends Exception> V
getChecked(Future<V> future, Class<X> exceptionClass)

Returns the result of Future#get(), converting most exceptions to a new instance of the given checked exception type.

public static <V extends @Nullable Object, X extends Exception> V
getChecked(Future<V> future, Class<X> exceptionClass, Duration timeout)

Returns the result of Future#get(long, TimeUnit), converting most exceptions to a new instance of the given checked exception type.

public static <V extends @Nullable Object, X extends Exception> V
getChecked(Future<V> future, Class<X> exceptionClass, long timeout, TimeUnit unit)

Returns the result of Future#get(long, TimeUnit), converting most exceptions to a new instance of the given checked exception type.

public static <V extends @Nullable Object> V
getDone(Future<V> future)

Returns the result of the input Future, which must have already completed.

public static <V extends @Nullable Object> V
getUnchecked(Future<V> future)

Returns the result of calling Future#get() uninterruptibly on a task known not to throw a checked exception.

private static <T extends @Nullable Object> ListenableFuture<? extends T>[]
gwtCompatibleToArray(Iterable<? extends ListenableFuture<? extends T>> futures)

Can't use Iterables.toArray because it's not gwt compatible

public static <V extends @Nullable Object> ListenableFuture<V>
immediateCancelledFuture()

Creates a ListenableFuture which is cancelled immediately upon construction, so that isCancelled() always returns true.

public static <V extends @Nullable Object> ListenableFuture<V>
immediateFailedFuture(Throwable throwable)

Returns a ListenableFuture which has an exception set immediately upon construction.

public static <V extends @Nullable Object> ListenableFuture<V>
immediateFuture(V value)

Creates a ListenableFuture which has its value set immediately upon construction.

public static ListenableFuture<@Nullable Void>
immediateVoidFuture()

Returns a successful ListenableFuture<Void>.

public static <T extends @Nullable Object> ImmutableList<ListenableFuture<T>>
inCompletionOrder(Iterable<? extends ListenableFuture<? extends T>> futures)

Returns a list of delegate futures that correspond to the futures received in the order that they complete.

public static <I extends @Nullable Object, O extends @Nullable Object> Future<O>

Returns:

A future that returns the result of the transformation.
lazyTransform
(final Future<I>
The future to transform
input
,
final Function<? super I, ? extends O>
A Function to transform the results of the provided future to the results of the returned future.
function
)

Like transform(ListenableFuture, Function, Executor) except that the transformation function is invoked on each call to get() on the returned future.

public static <V extends @Nullable Object> ListenableFuture<V>
nonCancellationPropagating(ListenableFuture<V> future)

Returns a ListenableFuture whose result is set from the supplied future when it completes.

public static <O extends @Nullable Object> ListenableFuture<O>
scheduleAsync(AsyncCallable<O> callable, Duration delay, ScheduledExecutorService executorService)

Schedules callable on the specified executor, returning a Future.

public static <O extends @Nullable Object> ListenableFuture<O>
scheduleAsync(AsyncCallable<O> callable, long delay, TimeUnit timeUnit, ScheduledExecutorService executorService)

Schedules callable on the specified executor, returning a Future.

public static <O extends @Nullable Object> ListenableFuture<O>
submit(Callable<O> callable, Executor executor)

Executes callable on the specified executor, returning a Future.

public static ListenableFuture<@Nullable Void>
submit(Runnable runnable, Executor executor)

Executes runnable on the specified executor, returning a Future that will complete after execution.

public static <O extends @Nullable Object> ListenableFuture<O>
submitAsync(AsyncCallable<O> callable, Executor executor)

Executes callable on the specified executor, returning a Future.

public static <V extends @Nullable Object> ListenableFuture<List<@Nullable V>>

Returns:

a future that provides a list of the results of the component futures
successfulAsList
(ListenableFuture<? extends V>...
futures to combine
futures
)

Creates a new ListenableFuture whose value is a list containing the values of all its successful input futures.

public static <V extends @Nullable Object> ListenableFuture<List<@Nullable V>>

Returns:

a future that provides a list of the results of the component futures
successfulAsList
(Iterable<? extends ListenableFuture<? extends V>>
futures to combine
futures
)

Creates a new ListenableFuture whose value is a list containing the values of all its successful input futures.

public static <I extends @Nullable Object, O extends @Nullable Object> ListenableFuture<O>

Returns:

A future that holds result of the transformation.
transform
(ListenableFuture<I>
The future to transform
input
,
Function<? super I, ? extends O>
A Function to transform the results of the provided future to the results of the returned future.
function
,
Executor
Executor to run the function in.
executor
)

Returns a new Future whose result is derived from the result of the given Future.

public static <I extends @Nullable Object, O extends @Nullable Object> ListenableFuture<O>

Returns:

A future that holds result of the function (if the input succeeded) or the original input's failure (if not)
transformAsync
(ListenableFuture<I>
The future to transform
input
,
AsyncFunction<? super I, ? extends O>
A function to transform the result of the input future to the result of the output future
function
,
Executor
Executor to run the function in.
executor
)

Returns a new Future whose result is asynchronously derived from the result of the given Future.

public static <V extends @Nullable Object> Futures.FutureCombiner<V>
whenAllComplete(ListenableFuture<? extends V>... futures)

Creates a FutureCombiner that processes the completed futures whether or not they're successful.

public static <V extends @Nullable Object> Futures.FutureCombiner<V>
whenAllComplete(Iterable<? extends ListenableFuture<? extends V>> futures)

Creates a FutureCombiner that processes the completed futures whether or not they're successful.

public static <V extends @Nullable Object> Futures.FutureCombiner<V>
whenAllSucceed(ListenableFuture<? extends V>... futures)

Creates a FutureCombiner requiring that all passed in futures are successful.

public static <V extends @Nullable Object> Futures.FutureCombiner<V>
whenAllSucceed(Iterable<? extends ListenableFuture<? extends V>> futures)

Creates a FutureCombiner requiring that all passed in futures are successful.

public static <V extends @Nullable Object> ListenableFuture<V>
withTimeout(ListenableFuture<V>
The future to delegate to.
delegate
,
Duration
when to time out the future
time
,
ScheduledExecutorService
The executor service to enforce the timeout.
scheduledExecutor
)

Returns a future that delegates to another but will finish early (via a TimeoutException wrapped in an ExecutionException) if the specified duration expires.

public static <V extends @Nullable Object> ListenableFuture<V>
withTimeout(ListenableFuture<V>
The future to delegate to.
delegate
,
long
when to time out the future
time
,
TimeUnit
the time unit of the time parameter
unit
,
ScheduledExecutorService
The executor service to enforce the timeout.
scheduledExecutor
)

Returns a future that delegates to another but will finish early (via a TimeoutException wrapped in an ExecutionException) if the specified duration expires.

private static void