Class Futures
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.
- Since:
- 1.0
- Author:
- Kevin Bourrillion, Nishant Thakkar, Sven Mawson
-
Method Summary
Modifier and TypeMethodDescriptionstatic <V> VReturns the result of the inputFuture, which must have already completed.static <V> ListenableFuture<V>immediateFailedFuture(Throwable throwable) Returns aListenableFuturewhich has an exception set immediately upon construction.static <V> ListenableFuture<V>immediateFuture(@Nullable V value) Creates aListenableFuturewhich has its value set immediately upon construction.static <I,O> ListenableFuture<O> transform(ListenableFuture<I> input, Function<? super I, ? extends O> function, Executor executor) Returns a newFuturewhose result is derived from the result of the givenFuture.static <V> ListenableFuture<V>withTimeout(ListenableFuture<V> delegate, long time, TimeUnit unit, ScheduledExecutorService scheduledExecutor) Returns a future that delegates to another but will finish early (via aTimeoutExceptionwrapped in anExecutionException) if the specified duration expires.
-
Method Details
-
immediateFuture
Creates aListenableFuturewhich has its value set immediately upon construction. The getters just return the value. ThisFuturecan't be canceled or timed out and itsisDone()method always returnstrue. -
immediateFailedFuture
Returns aListenableFuturewhich has an exception set immediately upon construction.The returned
Futurecan't be cancelled, and itsisDone()method always returnstrue. Callingget()will immediately throw the providedThrowablewrapped in anExecutionException. -
withTimeout
@Beta @GwtIncompatible public static <V> ListenableFuture<V> withTimeout(ListenableFuture<V> delegate, long time, TimeUnit unit, ScheduledExecutorService scheduledExecutor) Returns a future that delegates to another but will finish early (via aTimeoutExceptionwrapped in anExecutionException) if the specified duration expires.The delegate future is interrupted and cancelled if it times out.
- Parameters:
delegate- The future to delegate to.time- when to timeout the futureunit- the time unit of the time parameterscheduledExecutor- The executor service to enforce the timeout.- Since:
- 19.0
-
transform
@Beta public static <I,O> ListenableFuture<O> transform(ListenableFuture<I> input, Function<? super I, ? extends O> function, Executor executor) Returns a newFuturewhose result is derived from the result of the givenFuture. Ifinputfails, the returnedFuturefails with the same exception (and the function is not invoked). Example usage:ListenableFuture<QueryResult> queryFuture = ...; ListenableFuture<List<Row>> rowsFuture = transform(queryFuture, QueryResult::getRows, executor);When selecting an executor, note that
directExecutoris dangerous in some cases. See the discussion in theListenableFuture.addListenerdocumentation. All its warnings about heavyweight listeners are also applicable to heavyweight functions passed to this method.The returned
Futureattempts to keep its cancellation state in sync with that of the input future. That is, if the returnedFutureis cancelled, it will attempt to cancel the input, and if the input is cancelled, the returnedFuturewill receive a callback in which it will attempt to cancel itself.An example use of this method is to convert a serializable object returned from an RPC into a POJO.
- Parameters:
input- The future to transformfunction- A Function to transform the results of the provided future to the results of the returned future.executor- Executor to run the function in.- Returns:
- A future that holds result of the transformation.
- Since:
- 9.0 (in 2.0 as
compose)
-
getDone
Returns the result of the inputFuture, which must have already completed.The benefits of this method are twofold. First, the name "getDone" suggests to readers that the
Futureis already done. Second, if buggy code callsgetDoneon aFuturethat is still pending, the program will throw instead of block. This can be important for APIs likewhenAllComplete(...).call(...), where it is easy to use a new input from thecallimplementation but forget to add it to the arguments ofwhenAllComplete.If you are looking for a method to determine whether a given
Futureis done, use the instance methodFuture.isDone().- Throws:
ExecutionException- if theFuturefailed with an exceptionCancellationException- if theFuturewas cancelledIllegalStateException- if theFutureis not done- Since:
- 20.0
-