类 CompletableFutureCancellationHandler


  • public class CompletableFutureCancellationHandler
    extends java.lang.Object
    Implements cancellation and timeout support for CompletableFutures.

    This class ensures that the cancel action gets called once after the future completes with either CancellationException or TimeoutException. The implementation handles possible race conditions that might happen when the future gets cancelled before the cancel action is set to this handler.

    For timeouts, CompletableFuture's "orTimeout" method introduced in JDK9 can be used in client code.

    Cancellation and timeout support will only be active on the future where the cancellation handler has been attached to. Cancellation won't happen if .cancel is called on any "downstream" dependent futures. A cancellation or timeout that happens in any "upstream" future will get handled.

    • 方法概要

      所有方法 实例方法 具体方法 
      修饰符和类型 方法 说明
      void attachToFuture​(java.util.concurrent.CompletableFuture<?> future)
      Attaches the cancellation handler to handle cancels and timeouts.
      <T> java.util.concurrent.CompletableFuture<T> createFuture()
      Creates a new CompletableFuture and attaches the cancellation handler to handle cancels and timeouts.
      void setCancelAction​(java.lang.Runnable cancelAction)
      Set the action to run when the future gets cancelled or timeouts.
      • 从类继承的方法 java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • 构造器详细资料

      • CompletableFutureCancellationHandler

        public CompletableFutureCancellationHandler()
    • 方法详细资料

      • createFuture

        public <T> java.util.concurrent.CompletableFuture<T> createFuture()
        Creates a new CompletableFuture and attaches the cancellation handler to handle cancels and timeouts.
        类型参数:
        T - the result type of the future
        返回:
        a new future instance
      • attachToFuture

        public void attachToFuture​(java.util.concurrent.CompletableFuture<?> future)
        Attaches the cancellation handler to handle cancels and timeouts. A cancellation handler instance can be used only once.
        参数:
        future - the future to attach the handler to
      • setCancelAction

        public void setCancelAction​(java.lang.Runnable cancelAction)
        Set the action to run when the future gets cancelled or timeouts. The cancellation or timeout might be originating from any "upstream" future. The implementation ensures that the cancel action gets called once. Handles possible race conditions that might happen when the future gets cancelled before the cancel action is set to this handler. In this case, the cancel action gets called when the action is set.
        参数:
        cancelAction - the action to run when the the future gets cancelled or timeouts