Class Futures
ListenableFutures.
Mostly copied after Guava's Futures, because that one is still marked as beta
and is subject to change.
- Author:
- Guava
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <V> voidaddCallback(com.google.common.util.concurrent.ListenableFuture<V> future, com.google.common.util.concurrent.FutureCallback<? super V> callback) Deprecated.Registers separate success and failure callbacks to be run when theFuture's computation is complete or, if the computation is already complete, immediately.static <V> voidaddCallback(com.google.common.util.concurrent.ListenableFuture<V> future, com.google.common.util.concurrent.FutureCallback<? super V> callback, Executor executor) Deprecated.Registers separate success and failure callbacks to be run when theFuture's computation is complete or, if the computation is already complete, immediately.static <V> com.google.common.util.concurrent.ListenableFuture<List<V>>allAsList(com.google.common.util.concurrent.ListenableFuture<? extends V>... futures) Deprecated.Creates a newListenableFuturewhose value is a list containing the values of all its input futures, if all succeed.static <V> com.google.common.util.concurrent.ListenableFuture<List<V>>allAsList(Iterable<? extends com.google.common.util.concurrent.ListenableFuture<? extends V>> futures) Deprecated.Creates a newListenableFuturewhose value is a list containing the values of all its input futures, if all succeed.static <V> com.google.common.util.concurrent.ListenableFuture<V>immediateFailedFuture(Throwable throwable) Deprecated.Returns aListenableFuturewhich has an exception set immediately upon construction.static <V> com.google.common.util.concurrent.ListenableFuture<V>immediateFuture(V value) Deprecated.Creates aListenableFuturewhich has its value set immediately upon construction.static <I,O> com.google.common.util.concurrent.ListenableFuture<O> transform(com.google.common.util.concurrent.ListenableFuture<I> input, com.google.common.base.Function<? super I, ? extends O> function) Deprecated.Returns a newListenableFuturewhose result is the product of applying the givenFunctionto the result of the givenFuture.static <I,O> com.google.common.util.concurrent.ListenableFuture<O> transform(com.google.common.util.concurrent.ListenableFuture<I> input, com.google.common.base.Function<? super I, ? extends O> function, Executor executor) Deprecated.Returns a newListenableFuturewhose result is the product of applying the givenFunctionto the result of the givenFuture.static <I,O> com.google.common.util.concurrent.ListenableFuture<O> transform(com.google.common.util.concurrent.ListenableFuture<I> input, com.google.common.util.concurrent.AsyncFunction<? super I, ? extends O> function) Deprecated.Returns a newListenableFuturewhose result is asynchronously derived from the result of the givenFuture.static <I,O> com.google.common.util.concurrent.ListenableFuture<O> transform(com.google.common.util.concurrent.ListenableFuture<I> input, com.google.common.util.concurrent.AsyncFunction<? super I, ? extends O> function, Executor executor) Deprecated.Returns a newListenableFuturewhose result is asynchronously derived from the result of the givenFuture.
-
Constructor Details
-
Futures
public Futures()Deprecated.
-
-
Method Details
-
addCallback
public static <V> void addCallback(com.google.common.util.concurrent.ListenableFuture<V> future, com.google.common.util.concurrent.FutureCallback<? super V> callback) Deprecated.Registers separate success and failure callbacks to be run when theFuture's computation is complete or, if the computation is already complete, immediately.There is no guaranteed ordering of execution of callbacks, but any callback added through this method is guaranteed to be called once the computation is complete. Example:
Note: If the callback is slow or heavyweight, consider supplying an executor. If you do not supply an executor,ListenableFuture<QueryResult> future = ...; addCallback(future, new FutureCallback<QueryResult> { public void onSuccess(QueryResult result) { storeInCache(result); } public void onFailure(Throwable t) { reportError(t); } });addCallbackwill use a direct executor, which carries some caveats for heavier operations. For example, the callback may run on an unpredictable or undesirable thread:- If the input
Futureis done at the timeaddCallbackis called,addCallbackwill execute the callback inline. - If the input
Futureis not yet done,addCallbackwill schedule the callback to be run by the thread that completes the inputFuture, which may be an internal system thread such as an RPC network thread.
For a more general interface to attach a completion listener to a
Future, seeaddListener.- Parameters:
future- The future attach the callback to.callback- The callback to invoke whenfutureis completed.- Since:
- 10.0
- If the input
-
addCallback
public static <V> void addCallback(com.google.common.util.concurrent.ListenableFuture<V> future, com.google.common.util.concurrent.FutureCallback<? super V> callback, Executor executor) Deprecated.Registers separate success and failure callbacks to be run when theFuture's computation is complete or, if the computation is already complete, immediately.The callback is run in
executor. There is no guaranteed ordering of execution of callbacks, but any callback added through this method is guaranteed to be called once the computation is complete. Example:
When the callback is fast and lightweight, consider omitting the executor or explicitly specifyingListenableFuture<QueryResult> future = ...; Executor e = ... addCallback(future, e, new FutureCallback<QueryResult> { public void onSuccess(QueryResult result) { storeInCache(result); } public void onFailure(Throwable t) { reportError(t); } });directExecutor. However, be aware of the caveats documented in the link above.For a more general interface to attach a completion listener to a
Future, seeaddListener.- Parameters:
future- The future attach the callback to.callback- The callback to invoke whenfutureis completed.executor- The executor to runcallbackwhen the future completes.- Since:
- 10.0
-
immediateFuture
public static <V> com.google.common.util.concurrent.ListenableFuture<V> immediateFuture(@Nullable V value) Deprecated.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
public static <V> com.google.common.util.concurrent.ListenableFuture<V> immediateFailedFuture(Throwable throwable) Deprecated.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. -
transform
public static <I,O> com.google.common.util.concurrent.ListenableFuture<O> transform(com.google.common.util.concurrent.ListenableFuture<I> input, com.google.common.util.concurrent.AsyncFunction<? super I, ? extends O> function) Deprecated.Returns a newListenableFuturewhose result is asynchronously derived from the result of the givenFuture. More precisely, the returnedFuturetakes its result from aFutureproduced by applying the givenAsyncFunctionto the result of the originalFuture. Example:
Note: If the derivedListenableFuture<RowKey> rowKeyFuture = indexService.lookUp(query); AsyncFunction<RowKey, QueryResult> queryFunction = new AsyncFunction<RowKey, QueryResult>() { public ListenableFuture<QueryResult> apply(RowKey rowKey) { return dataService.read(rowKey); } }; ListenableFuture<QueryResult> queryFuture = transform(rowKeyFuture, queryFunction);Futureis slow or heavyweight to create (whether theFutureitself is slow or heavyweight to complete is irrelevant), consider supplying an executor. If you do not supply an executor,transformwill use a direct executor, which carries some caveats for heavier operations. For example, the call tofunction.applymay run on an unpredictable or undesirable thread:- If the input
Futureis done at the timetransformis called,transformwill callfunction.applyinline. - If the input
Futureis not yet done,transformwill schedulefunction.applyto be run by the thread that completes the inputFuture, which may be an internal system thread such as an RPC network thread.
function.apply, all other registered but unexecuted listeners are prevented from running during its execution, even if those listeners are to run in other executors.The returned
Futureattempts to keep its cancellation state in sync with that of the input future and that of the future returned by the function. That is, if the returnedFutureis cancelled, it will attempt to cancel the other two, and if either of the other two is cancelled, the returnedFuturewill receive a callback in which it will attempt to cancel itself.- Parameters:
input- The future to transformfunction- A function to transform the result of the input future to the result of the output future- Returns:
- A future that holds result of the function (if the input succeeded) or the original input's failure (if not)
- Since:
- 11.0
- If the input
-
transform
public static <I,O> com.google.common.util.concurrent.ListenableFuture<O> transform(com.google.common.util.concurrent.ListenableFuture<I> input, com.google.common.util.concurrent.AsyncFunction<? super I, ? extends O> function, Executor executor) Deprecated.Returns a newListenableFuturewhose result is asynchronously derived from the result of the givenFuture. More precisely, the returnedFuturetakes its result from aFutureproduced by applying the givenAsyncFunctionto the result of the originalFuture. Example:ListenableFuture<RowKey> rowKeyFuture = indexService.lookUp(query); AsyncFunction<RowKey, QueryResult> queryFunction = new AsyncFunction<RowKey, QueryResult>() { public ListenableFuture<QueryResult> apply(RowKey rowKey) { return dataService.read(rowKey); } }; ListenableFuture<QueryResult> queryFuture = transform(rowKeyFuture, queryFunction, executor);The returned
Futureattempts to keep its cancellation state in sync with that of the input future and that of the future returned by the chain function. That is, if the returnedFutureis cancelled, it will attempt to cancel the other two, and if either of the other two is cancelled, the returnedFuturewill receive a callback in which it will attempt to cancel itself.When the execution of
function.applyis fast and lightweight (though theFutureit returns need not meet these criteria), consider omitting the executor or explicitly specifyingdirectExecutor. However, be aware of the caveats documented in the link above.- Parameters:
input- The future to transformfunction- A function to transform the result of the input future to the result of the output futureexecutor- Executor to run the function in.- Returns:
- A future that holds result of the function (if the input succeeded) or the original input's failure (if not)
- Since:
- 11.0
-
transform
public static <I,O> com.google.common.util.concurrent.ListenableFuture<O> transform(com.google.common.util.concurrent.ListenableFuture<I> input, com.google.common.base.Function<? super I, ? extends O> function) Deprecated.Returns a newListenableFuturewhose result is the product of applying the givenFunctionto the result of the givenFuture. Example:
Note: If the transformation is slow or heavyweight, consider supplying an executor. If you do not supply an executor,ListenableFuture<QueryResult> queryFuture = ...; Function<QueryResult, List<Row>> rowsFunction = new Function<QueryResult, List<Row>>() { public List<Row> apply(QueryResult queryResult) { return queryResult.getRows(); } }; ListenableFuture<List<Row>> rowsFuture = transform(queryFuture, rowsFunction);transformwill use an inline executor, which carries some caveats for heavier operations. For example, the call tofunction.applymay run on an unpredictable or undesirable thread:- If the input
Futureis done at the timetransformis called,transformwill callfunction.applyinline. - If the input
Futureis not yet done,transformwill schedulefunction.applyto be run by the thread that completes the inputFuture, which may be an internal system thread such as an RPC network thread.
function.apply, all other registered but unexecuted listeners are prevented from running during its execution, even if those listeners are to run in other executors.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. This will be run in the thread that notifies input it is complete.- Returns:
- A future that holds result of the transformation.
- Since:
- 9.0 (in 1.0 as
compose)
- If the input
-
transform
public static <I,O> com.google.common.util.concurrent.ListenableFuture<O> transform(com.google.common.util.concurrent.ListenableFuture<I> input, com.google.common.base.Function<? super I, ? extends O> function, Executor executor) Deprecated.Returns a newListenableFuturewhose result is the product of applying the givenFunctionto the result of the givenFuture. Example:ListenableFuture<QueryResult> queryFuture = ...; Function<QueryResult, List<Row>> rowsFunction = new Function<QueryResult, List<Row>>() { public List<Row> apply(QueryResult queryResult) { return queryResult.getRows(); } }; ListenableFuture<List<Row>> rowsFuture = transform(queryFuture, rowsFunction, executor);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.
When the transformation is fast and lightweight, consider omitting the executor or explicitly specifying
directExecutor. However, be aware of the caveats documented in the link above.- 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)
-
allAsList
@Beta public static <V> com.google.common.util.concurrent.ListenableFuture<List<V>> allAsList(com.google.common.util.concurrent.ListenableFuture<? extends V>... futures) Deprecated.Creates a newListenableFuturewhose value is a list containing the values of all its input futures, if all succeed. If any input fails, the returned future fails.The list of results is in the same order as the input list.
Canceling this future does not cancel any of the component futures; however, if any of the provided futures fails or is canceled, this one is, too.
- Parameters:
futures- futures to combine- Returns:
- a future that provides a list of the results of the component futures
- Since:
- 10.0
-
allAsList
@Beta public static <V> com.google.common.util.concurrent.ListenableFuture<List<V>> allAsList(Iterable<? extends com.google.common.util.concurrent.ListenableFuture<? extends V>> futures) Deprecated.Creates a newListenableFuturewhose value is a list containing the values of all its input futures, if all succeed. If any input fails, the returned future fails.The list of results is in the same order as the input list.
Canceling this future does not cancel any of the component futures; however, if any of the provided futures fails or is canceled, this one is, too.
- Parameters:
futures- futures to combine- Returns:
- a future that provides a list of the results of the component futures
- Since:
- 10.0
-