Module aeonics.core

Class Executor.Task<T>

  • Type Parameters:
    T - the return type of the task
    Enclosing class:
    Executor

    public static final class Executor.Task<T>
    extends java.lang.Object
    This class represents a task that is running, is done or will run in the future. It is a simplified wrapper around CompletableFuture designed to be used by the Executor implementations.
    • Method Detail

      • completed

        protected static <T> Executor.Task<T> completed​(T value,
                                                        java.util.concurrent.Executor executor)
        Returns an already completed task.
        Type Parameters:
        T - the return type
        Parameters:
        value - the completion value
        executor - the executor in which subsequent tasks should run
        Returns:
        an already completed task
      • failed

        protected static Executor.Task<java.lang.Void> failed​(java.lang.Throwable value,
                                                              java.util.concurrent.Executor executor)
        Returns an already failed task.
        Parameters:
        value - the failure cause
        executor - the executor in which subsequent tasks should run
        Returns:
        an already failed task
      • sync

        protected static <U> Executor.Task<U> sync​(Functions.Supplier<U> task,
                                                   java.util.concurrent.Executor executor)
        Schedules the given task to run in the specified executor. Any then(aeonics.util.Functions.Runnable), or(aeonics.util.Functions.Runnable) (or variants) methods will run synchronously in the same thread as the task.
        Type Parameters:
        U - the return type of the task
        Parameters:
        task - the task to run
        executor - the executor in which the task should run
        Returns:
        the task object to eventually chain or cancel operations
      • async

        protected static <U> Executor.Task<U> async​(Functions.Supplier<U> task,
                                                    java.util.concurrent.Executor executor)
        Schedules the given task to run in the specified executor. Any then(aeonics.util.Functions.Runnable), or(aeonics.util.Functions.Runnable) (or variants) methods will be rescheduled asynchronously in the same executor as the task.
        Type Parameters:
        U - the return type of the task
        Parameters:
        task - the task to run
        executor - the executor in which the task should run
        Returns:
        the task object to eventually chain or cancel operations
      • then

        public Executor.Task<java.lang.Void> then​(Functions.Runnable task)
        Schedules another task to run upon successful completion of this task
        Parameters:
        task - the other task
        Returns:
        a new task object to eventually chain or cancel operations
      • then

        public Executor.Task<java.lang.Void> then​(Functions.Consumer<? super T> task)
        Schedules another task to run upon successful completion of this task
        Parameters:
        task - the other task
        Returns:
        a new task object to eventually chain or cancel operations
      • then

        public <U> Executor.Task<U> then​(Functions.Supplier<? extends U> task)
        Schedules another task to run upon successful completion of this task
        Type Parameters:
        U - the task return type
        Parameters:
        task - the other task
        Returns:
        a new task object to eventually chain or cancel operations
      • then

        public <U> Executor.Task<U> then​(Functions.Function<? super T,​? extends U> task)
        Schedules another task to run upon successful completion of this task
        Type Parameters:
        U - the task return type
        Parameters:
        task - the other task
        Returns:
        a new task object to eventually chain or cancel operations
      • or

        public Executor.Task<T> or​(Functions.Runnable task)
        Schedules another task to run if this task threw an exception. This stage completes with a null result. If you wish to interrupt processing, then simply rethrow the exception.
        Parameters:
        task - the other task
        Returns:
        a new task object to eventually chain or cancel operations
      • or

        public Executor.Task<T> or​(Functions.Consumer<java.lang.Throwable> task)
        Schedules another task to run if this task threw an exception. This stage completes with a null result. If you wish to interrupt processing, then simply rethrow the exception.
        Parameters:
        task - the other task
        Returns:
        a new task object to eventually chain or cancel operations
      • or

        public Executor.Task<T> or​(Functions.Supplier<? extends T> task)
        Schedules another task to run if this task threw an exception. This stage completes with the returned value. If you wish to interrupt processing, then simply rethrow the exception.
        Parameters:
        task - the other task
        Returns:
        a new task object to eventually chain or cancel operations
      • or

        public Executor.Task<T> or​(Functions.Function<java.lang.Throwable,​? extends T> task)
        Schedules another task to run if this task threw an exception. This stage completes with the returned value. If you wish to interrupt processing, then simply rethrow the exception.
        Parameters:
        task - the other task
        Returns:
        a new task object to eventually chain or cancel operations
      • anyway

        public Executor.Task<java.lang.Void> anyway​(Functions.Runnable task)
        Schedules another task to run upon completion of this task regardless of the successful or failure.
        Parameters:
        task - the other task
        Returns:
        a new task object to eventually chain or cancel operations
      • anyway

        public Executor.Task<java.lang.Void> anyway​(Functions.BiConsumer<T,​java.lang.Throwable> task)
        Schedules another task to run upon completion of this task regardless of the successful or failure.

        If the second parameter (the exception) is null, it indicates a successful operation and the first parameter (the return value) is filled. That value may be null too.

        If the second parameter (the exception) is not null, it indicates a failed operation and the first parameter (the return value) is null.

        Parameters:
        task - the other task
        Returns:
        a new task object to eventually chain or cancel operations
      • anyway

        public <U> Executor.Task<U> anyway​(Functions.Supplier<? extends U> task)
        Schedules another task to run upon completion of this task regardless of the successful or failure.
        Type Parameters:
        U - the task return type
        Parameters:
        task - the other task
        Returns:
        a new task object to eventually chain or cancel operations
      • anyway

        public <U> Executor.Task<U> anyway​(Functions.BiFunction<T,​java.lang.Throwable,​? extends U> task)
        Schedules another task to run upon completion of this task regardless of the successful or failure.

        If the second parameter (the exception) is null, it indicates a successful operation and the first parameter (the return value) is filled. That value may be null too.

        If the second parameter (the exception) is not null, it indicates a failed operation and the first parameter (the return value) is null.

        Type Parameters:
        U - the task return type
        Parameters:
        task - the other task
        Returns:
        a new task object to eventually chain or cancel operations
      • link

        public <U> Executor.Task<U> link​(Executor.Task<U> task)
        Link the completion of the current task to that of the specified one.

        The linked task will only execute in case of successful completion of this one.

        If this task completes successfully and the linked task too, then the result is the linked task's value. This operation is non-blocking and the returned task will be pending until then.

        Beware that this operation may cause a dead lock in case linking a task that is enqueued and has no chance to execute.
        Type Parameters:
        U - the task return type
        Parameters:
        task - the other task
        Returns:
        a new task object to eventually chain or cancel operations
      • link

        public <U> Executor.Task<U> link​(Functions.Function<T,​Executor.Task<U>> task)
        Link the completion of the current task to that of the specified one.

        The linked task will only execute in case of successful completion of this one.

        If this task completes successfully and the linked task too, then the result is the linked task's value. This operation is non-blocking and the returned task will be pending until then.

        Beware that this operation may cause a dead lock in case linking a task that is enqueued and has no chance to execute.
        Type Parameters:
        U - the task return type
        Parameters:
        task - the other task
        Returns:
        a new task object to eventually chain or cancel operations
      • await

        public T await()
        Waits for the result of this task to be available and returns the value. Beware that this operation may cause a dead lock in case awaiting a task that is enqueued and has no chance to execute.

        It is generally bad practice to use this method unless you know exactly what you are doing.

        Returns:
        the output value of the task
        Throws:
        java.util.concurrent.CompletionException - if the task itself threw an exception
        java.util.concurrent.CancellationException - if the task was cancelled using cancel()
      • all

        protected static Executor.Task<java.lang.Void> all​(java.util.List<Executor.Task<?>> tasks,
                                                           java.util.concurrent.Executor executor)
        Returns a new task that completes when all tasks are completed (successfully or not). If you are interested by the result of individual tasks, you should inspect each of them after the returned task completes. Beware that this operation may cause a dead lock in case providing a task that is enqueued and has no chance to execute.
        Parameters:
        tasks - the list of tasks to group together
        executor - the executor in which subsequent tasks should run
        Returns:
        a new task that completes when all tasks are completed
      • cancel

        public void cancel()
        Cancels this task if it is not done yet. This method applies to this task only and not the parent tasks.