Class DeferredFutureTask<D,​P>

  • Type Parameters:
    D - Type used for Deferred.resolve(Object)
    P - Type used for Deferred.notify(Object)
    All Implemented Interfaces:
    java.lang.Runnable, java.util.concurrent.Future<D>, java.util.concurrent.RunnableFuture<D>

    public class DeferredFutureTask<D,​P>
    extends java.util.concurrent.FutureTask<D>
    FutureTask can wrap around Callable and Runnable. In these two cases, a new Deferred object will be created. This class will override FutureTask.done() to trigger the appropriate Deferred actions.

    Note, type used for Deferred.reject(Object) is always Throwable.

    When the task is completed successfully, Deferred.resolve(Object) will be called. When a task is canceled, Deferred.reject(Object) will be called with an instance of CancellationException If any Exception occurred, Deferred.reject(Object) will be called with the Exception instance.

    • Constructor Summary

      Constructors 
      Constructor Description
      DeferredFutureTask​(java.lang.Runnable task)
      Creates a new DeferredFutureTask with the given task.
      DeferredFutureTask​(java.lang.Runnable task, CancellationHandler cancellationHandler)
      Creates a new DeferredFutureTask with the given task and a explicit CancellationHandler The given cancellationHandler has precedence over the given task if the task implements the CancellationHandler interface.
      DeferredFutureTask​(java.util.concurrent.Callable<D> task)
      Creates a new DeferredFutureTask with the given task.
      DeferredFutureTask​(java.util.concurrent.Callable<D> task, CancellationHandler cancellationHandler)
      Creates a new DeferredFutureTask with the given task and a explicit CancellationHandler The given cancellationHandler has precedence over the given task if the task implements the CancellationHandler interface.
      DeferredFutureTask​(DeferredCallable<D,​P> task)
      Creates a new DeferredFutureTask with the given task.
      DeferredFutureTask​(DeferredCallable<D,​P> task, CancellationHandler cancellationHandler)
      Creates a new DeferredFutureTask with the given task and a explicit CancellationHandler The given cancellationHandler has precedence over the given task if the task implements the CancellationHandler interface.
      DeferredFutureTask​(DeferredRunnable<P> task)
      Creates a new DeferredFutureTask with the given task.
      DeferredFutureTask​(DeferredRunnable<P> task, CancellationHandler cancellationHandler)
      Creates a new DeferredFutureTask with the given task and a explicit CancellationHandler The given cancellationHandler has precedence over the given task if the task implements the CancellationHandler interface.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected java.lang.Throwable causeOf​(java.lang.Exception e)  
      protected void cleanup()
      Performs resource cleanup upon interruption or cancellation of the underlying task.
      protected void done()  
      DeferredManager.StartPolicy getStartPolicy()  
      Promise<D,​java.lang.Throwable,​P> promise()  
      • Methods inherited from class java.util.concurrent.FutureTask

        cancel, get, get, isCancelled, isDone, run, runAndReset, set, setException, toString
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • LOG

        private static final org.slf4j.Logger LOG
      • deferred

        protected final Deferred<D,​java.lang.Throwable,​P> deferred
      • taskDelegate

        private java.lang.Object taskDelegate
    • Constructor Detail

      • DeferredFutureTask

        public DeferredFutureTask​(java.util.concurrent.Callable<D> task)
        Creates a new DeferredFutureTask with the given task. The given task may implement the CancellationHandler interface.
        Parameters:
        task - the task to be executed. Must not be null.
      • DeferredFutureTask

        public DeferredFutureTask​(java.lang.Runnable task)
        Creates a new DeferredFutureTask with the given task. The given task may implement the CancellationHandler interface.
        Parameters:
        task - the task to be executed. Must not be null.
      • DeferredFutureTask

        public DeferredFutureTask​(DeferredCallable<D,​P> task)
        Creates a new DeferredFutureTask with the given task. The given task may implement the CancellationHandler interface.
        Parameters:
        task - the task to be executed. Must not be null.
      • DeferredFutureTask

        public DeferredFutureTask​(DeferredRunnable<P> task)
        Creates a new DeferredFutureTask with the given task. The given task may implement the CancellationHandler interface.
        Parameters:
        task - the task to be executed. Must not be null.
      • DeferredFutureTask

        public DeferredFutureTask​(java.util.concurrent.Callable<D> task,
                                  CancellationHandler cancellationHandler)
        Creates a new DeferredFutureTask with the given task and a explicit CancellationHandler The given cancellationHandler has precedence over the given task if the task implements the CancellationHandler interface.
        Parameters:
        task - the task to be executed. Must not be null.
        cancellationHandler - the CancellationHandler to invoke during onCancel. May be null.
        Since:
        2.0
      • DeferredFutureTask

        public DeferredFutureTask​(java.lang.Runnable task,
                                  CancellationHandler cancellationHandler)
        Creates a new DeferredFutureTask with the given task and a explicit CancellationHandler The given cancellationHandler has precedence over the given task if the task implements the CancellationHandler interface.
        Parameters:
        task - the task to be executed. Must not be null.
        cancellationHandler - the CancellationHandler to invoke during onCancel. May be null.
        Since:
        2.0
      • DeferredFutureTask

        public DeferredFutureTask​(DeferredCallable<D,​P> task,
                                  CancellationHandler cancellationHandler)
        Creates a new DeferredFutureTask with the given task and a explicit CancellationHandler The given cancellationHandler has precedence over the given task if the task implements the CancellationHandler interface.
        Parameters:
        task - the task to be executed. Must not be null.
        cancellationHandler - the CancellationHandler to invoke during onCancel. May be null.
        Since:
        2.0
      • DeferredFutureTask

        public DeferredFutureTask​(DeferredRunnable<P> task,
                                  CancellationHandler cancellationHandler)
        Creates a new DeferredFutureTask with the given task and a explicit CancellationHandler The given cancellationHandler has precedence over the given task if the task implements the CancellationHandler interface.
        Parameters:
        task - the task to be executed. Must not be null.
        cancellationHandler - the CancellationHandler to invoke during onCancel. May be null.
        Since:
        2.0
    • Method Detail

      • promise

        public Promise<D,​java.lang.Throwable,​P> promise()
      • done

        protected void done()
        Overrides:
        done in class java.util.concurrent.FutureTask<D>
      • causeOf

        protected java.lang.Throwable causeOf​(java.lang.Exception e)
      • cleanup

        protected void cleanup()
        Performs resource cleanup upon interruption or cancellation of the underlying task. This method gives precedence to cancellationHandler it not null, otherwise it invokes the underlying task's onCancel() if it implements the CancellationHandler interface.
        Since:
        2.0