接口 ListenableFuture<T>
- 类型参数:
T- the result type returned by this Future'sgetmethod
- 所有超级接口:
Future<T>
- 所有已知子接口:
ProgressiveFuture<V>,SettableFuture<V>
- 所有已知实现类:
AbstractFuture,CompleteFuture,DefaultFuture,DefaultProgressiveFuture,FailedFuture,ListenableFutureTask,SettableFutureTask,SucceededFuture
Future with the capability to accept completion callbacks.
If the future has completed when the callback is added, the callback is
triggered immediately.
Inspired by com.google.common.util.concurrent.ListenableFuture.
and io.netty.util.concurrent.Future
- 从以下版本开始:
- 4.0
- 作者:
- Arjen Poutsma, Sebastien Deleuze, Harry Yang, Juergen Hoeller
-
方法概要
修饰符和类型方法说明addListener(FutureListener<? extends ListenableFuture<T>> listener) Adds the specified listener to this future.default ListenableFuture<T>addListener(SuccessCallback<T> successCallback, FailureCallback failureCallback) Java 8 lambda-friendly alternative with success and failure callbacks.addListeners(FutureListener<? extends ListenableFuture<T>>... listeners) Adds the specified listeners to this future.await()Waits for this future to be completed.booleanawait(long timeoutMillis) Waits for this future to be completed within the specified time limit.booleanWaits for this future to be completed within the specified time limit.Waits for this future to be completed without interruption.booleanawaitUninterruptibly(long timeoutMillis) Waits for this future to be completed within the specified time limit without interruption.booleanawaitUninterruptibly(long timeout, TimeUnit unit) Waits for this future to be completed within the specified time limit without interruption.booleancancel(boolean mayInterruptIfRunning) If the cancellation was successful it will fail the future with aCancellationException.default CompletableFuture<T>Expose thisListenableFutureas a JDKCompletableFuture.static <V> FailedFuture<V>Creates a new FailedFuture instance.static <V> FailedFuture<V>Creates a new FailedFuture instance.static <V> SettableFuture<V>Creates a new SettableFuture instance.static <V> SettableFuture<V>forSettable(Executor executor) Creates a new SettableFuture instance.static <V> SucceededFuture<V>forSucceeded(Executor executor, V result) Creates a new SucceededFuture instance.static <V> SucceededFuture<V>forSucceeded(V result) Creates a new SucceededFuture instance.getCause()Returns the cause of the failed operation if the operation failed.getNow()Return the result without blocking.booleanbooleanReturnstrueif and only if the operation was completed successfully.obtain()Return the result without blocking.default ListenableFuture<T>onFailure(FailureCallback failureCallback) Java 8 lambda-friendly alternative with failure callbacks.default ListenableFuture<T>onSuccess(SuccessCallback<T> successCallback) Java 8 lambda-friendly alternative with success callbacks.removeListener(FutureListener<? extends ListenableFuture<T>> listener) Removes the first occurrence of the specified listener from this future.removeListeners(FutureListener<? extends ListenableFuture<T>>... listeners) Removes the first occurrence for each of the listeners from this future.sync()Waits for this future until it is done, and rethrows the cause of the failure if this future failed.Waits for this future until it is done, and rethrows the cause of the failure if this future failed.从接口继承的方法 java.util.concurrent.Future
get, get, isCancelled, isDone
-
方法详细资料
-
isSuccess
boolean isSuccess()Returnstrueif and only if the operation was completed successfully. -
isCancellable
boolean isCancellable()- 返回:
- returns
trueif and only if the operation can be cancelled viacancel(boolean).
-
getCause
Returns the cause of the failed operation if the operation failed.- 返回:
- the cause of the failure.
nullif succeeded or this future is not completed yet.
-
addListener
default ListenableFuture<T> addListener(SuccessCallback<T> successCallback, @Nullable FailureCallback failureCallback) Java 8 lambda-friendly alternative with success and failure callbacks.- 参数:
successCallback- the success callbackfailureCallback- the failure callback
-
onSuccess
Java 8 lambda-friendly alternative with success callbacks.- 参数:
successCallback- the success callback
-
onFailure
Java 8 lambda-friendly alternative with failure callbacks.- 参数:
failureCallback- the failure callback
-
addListener
Adds the specified listener to this future.The specified listener is notified when this future is done. If this future is already completed, the specified listener is notified immediately.
-
addListeners
Adds the specified listeners to this future.The specified listeners are notified when this future is done. If this future is already completed, the specified listeners are notified immediately.
-
removeListener
Removes the first occurrence of the specified listener from this future.The specified listener is no longer notified when this future is done. If the specified listener is not associated with this future, this method does nothing and returns silently.
-
removeListeners
Removes the first occurrence for each of the listeners from this future.The specified listeners are no longer notified when this future is done. If the specified listeners are not associated with this future, this method does nothing and returns silently.
-
sync
Waits for this future until it is done, and rethrows the cause of the failure if this future failed. -
syncUninterruptibly
ListenableFuture<T> syncUninterruptibly()Waits for this future until it is done, and rethrows the cause of the failure if this future failed. -
await
Waits for this future to be completed.- 抛出:
InterruptedException- if the current thread was interrupted
-
awaitUninterruptibly
ListenableFuture<T> awaitUninterruptibly()Waits for this future to be completed without interruption. This method catches anInterruptedExceptionand discards it silently. -
await
Waits for this future to be completed within the specified time limit.- 返回:
trueif and only if the future was completed within the specified time limit- 抛出:
InterruptedException- if the current thread was interrupted
-
await
Waits for this future to be completed within the specified time limit.- 返回:
trueif and only if the future was completed within the specified time limit- 抛出:
InterruptedException- if the current thread was interrupted
-
awaitUninterruptibly
Waits for this future to be completed within the specified time limit without interruption. This method catches anInterruptedExceptionand discards it silently.- 返回:
trueif and only if the future was completed within the specified time limit
-
awaitUninterruptibly
boolean awaitUninterruptibly(long timeoutMillis) Waits for this future to be completed within the specified time limit without interruption. This method catches anInterruptedExceptionand discards it silently.- 返回:
trueif and only if the future was completed within the specified time limit
-
getNow
Return the result without blocking. If the future is not done yet this will returnnull.As it is possible that a
nullvalue is used to mark the future as successful you also need to check if the future is really done withFuture.isDone()and not rely on the returnednullvalue. -
obtain
Return the result without blocking.must invoke after
isSuccess()- 抛出:
IllegalStateException-SettableFuture.setSuccess(Object)is setnull- 另请参阅:
-
cancel
boolean cancel(boolean mayInterruptIfRunning) If the cancellation was successful it will fail the future with aCancellationException. -
completable
Expose thisListenableFutureas a JDKCompletableFuture. -
forSettable
Creates a new SettableFuture instance. -
forSettable
Creates a new SettableFuture instance.- 参数:
executor- theExecutorwhich is used to notify the SettableFuture once it is complete.
-
forFailed
Creates a new FailedFuture instance. -
forFailed
Creates a new FailedFuture instance.- 参数:
executor- theExecutorwhich is used to notify the Future once it is complete.
-
forSucceeded
Creates a new SucceededFuture instance. -
forSucceeded
Creates a new SucceededFuture instance.- 参数:
executor- theExecutorwhich is used to notify the Future once it is complete.
-