Class CompletableFutureTask<R>

java.lang.Object
java.util.concurrent.CompletableFuture<R>
com.powsybl.computation.CompletableFutureTask<R>
All Implemented Interfaces:
Runnable, CompletionStage<R>, Future<R>

public class CompletableFutureTask<R> extends CompletableFuture<R> implements Runnable
A CompletableFuture which embeds an actual task, unlike the default implementation. In particular, the behaviour of cancel(boolean) is modified in order to actually interrupt the thread executing the bound task.

Similarly to FutureTask, this class implements Runnable and can therefore be submitted to an Executor for execution.

Author:
Sylvain Leclerc <sylvain.leclerc at rte-france.com>
  • Constructor Details

    • CompletableFutureTask

      public CompletableFutureTask(Callable<R> task)
      Creates a completable future bound to the specified task. This task will be executed on a call to run(), for instance by submitting this object to an Executor.
      Parameters:
      task - The task to be bound to this future.
  • Method Details

    • run

      public void run()
      Executes the wrapped callable. On execution end, this future will complete with the callable result, or complete exceptionally in case of exception.
      Specified by:
      run in interface Runnable
    • runAsync

      public CompletableFutureTask<R> runAsync(Executor executor)
      Submits this task to the specified executor.
      Parameters:
      executor - The executor which will execute this task.
      Returns:
      This.
    • runAsync

      public static <T> CompletableFutureTask<T> runAsync(Callable<T> task, Executor executor)
      Creates a completable future task and submits it to the specified executor.
      Parameters:
      task - The task to be executed.
      executor - The executor which will execute the task.
      Returns:
      A CompletableFutureTask bound to the specified task.
    • cancel

      public boolean cancel(boolean mayInterruptIfRunning)
      Cancels this completable future and, if requested and if the bound task is under execution, interrupts the thread executing the task.
      Specified by:
      cancel in interface Future<R>
      Overrides:
      cancel in class CompletableFuture<R>
      Parameters:
      mayInterruptIfRunning - if true, and if the bound task is under execution, the thread executing the task will be interrupted
      Returns:
      false if the task could not be cancelled, typically because it has already completed; true otherwise
    • newIncompleteFuture

      public <U> CompletableFuture<U> newIncompleteFuture()
      Overrides:
      newIncompleteFuture in class CompletableFuture<R>