接口 ListenableFuture<T>

类型参数:
T - the result type returned by this Future's get method
所有超级接口:
Future<T>
所有已知子接口:
ProgressiveFuture<V>, SettableFuture<V>
所有已知实现类:
AbstractFuture, CompleteFuture, DefaultFuture, DefaultProgressiveFuture, FailedFuture, ListenableFutureTask, SettableFutureTask, SucceededFuture

public interface ListenableFuture<T> extends Future<T>
Extend 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
  • 方法详细资料

    • isSuccess

      boolean isSuccess()
      Returns true if and only if the operation was completed successfully.
    • isCancellable

      boolean isCancellable()
      返回:
      returns true if and only if the operation can be cancelled via cancel(boolean).
    • getCause

      @Nullable Throwable getCause()
      Returns the cause of the failed operation if the operation failed.
      返回:
      the cause of the failure. null if 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 callback
      failureCallback - the failure callback
    • onSuccess

      default ListenableFuture<T> onSuccess(SuccessCallback<T> successCallback)
      Java 8 lambda-friendly alternative with success callbacks.
      参数:
      successCallback - the success callback
    • onFailure

      default ListenableFuture<T> onFailure(FailureCallback failureCallback)
      Java 8 lambda-friendly alternative with failure callbacks.
      参数:
      failureCallback - the failure callback
    • addListener

      ListenableFuture<T> addListener(FutureListener<? extends ListenableFuture<T>> listener)
      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

      ListenableFuture<T> addListeners(FutureListener<? extends ListenableFuture<T>>... listeners)
      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

      ListenableFuture<T> removeListener(FutureListener<? extends ListenableFuture<T>> listener)
      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

      ListenableFuture<T> removeListeners(FutureListener<? extends ListenableFuture<T>>... listeners)
      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.
      抛出:
      InterruptedException
    • 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 an InterruptedException and discards it silently.
    • await

      boolean await(long timeout, TimeUnit unit) throws InterruptedException
      Waits for this future to be completed within the specified time limit.
      返回:
      true if and only if the future was completed within the specified time limit
      抛出:
      InterruptedException - if the current thread was interrupted
    • await

      boolean await(long timeoutMillis) throws InterruptedException
      Waits for this future to be completed within the specified time limit.
      返回:
      true if and only if the future was completed within the specified time limit
      抛出:
      InterruptedException - if the current thread was interrupted
    • awaitUninterruptibly

      boolean awaitUninterruptibly(long timeout, TimeUnit unit)
      Waits for this future to be completed within the specified time limit without interruption. This method catches an InterruptedException and discards it silently.
      返回:
      true if 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 an InterruptedException and discards it silently.
      返回:
      true if and only if the future was completed within the specified time limit
    • getNow

      @Nullable T getNow()
      Return the result without blocking. If the future is not done yet this will return null.

      As it is possible that a null value is used to mark the future as successful you also need to check if the future is really done with Future.isDone() and not rely on the returned null value.

    • obtain

      T obtain() throws IllegalStateException
      Return the result without blocking.

      must invoke after isSuccess()

      抛出:
      IllegalStateException - SettableFuture.setSuccess(Object) is set null
      另请参阅:
    • cancel

      boolean cancel(boolean mayInterruptIfRunning)
      If the cancellation was successful it will fail the future with a CancellationException.
      指定者:
      cancel 在接口中 Future<T>
    • completable

      default CompletableFuture<T> completable()
      Expose this ListenableFuture as a JDK CompletableFuture.
    • forSettable

      static <V> SettableFuture<V> forSettable()
      Creates a new SettableFuture instance.
    • forSettable

      static <V> SettableFuture<V> forSettable(@Nullable Executor executor)
      Creates a new SettableFuture instance.
      参数:
      executor - the Executor which is used to notify the SettableFuture once it is complete.
    • forFailed

      static <V> FailedFuture<V> forFailed(Throwable cause)
      Creates a new FailedFuture instance.
    • forFailed

      static <V> FailedFuture<V> forFailed(@Nullable Executor executor, Throwable cause)
      Creates a new FailedFuture instance.
      参数:
      executor - the Executor which is used to notify the Future once it is complete.
    • forSucceeded

      static <V> SucceededFuture<V> forSucceeded(@Nullable V result)
      Creates a new SucceededFuture instance.
    • forSucceeded

      static <V> SucceededFuture<V> forSucceeded(@Nullable Executor executor, @Nullable V result)
      Creates a new SucceededFuture instance.
      参数:
      executor - the Executor which is used to notify the Future once it is complete.