Task

public final class Task<Output> : FutureProtocol, Cancellable

A handle to a unit of work that is executing in an executor.

In Futures, task is both a high-level concept and a type. As a concept, it is the outermost future that is submitted into an executor for execution. As a type, it is a handle to the conceptual task, which can be used to extract the result of the computation or cancel it altogether.

Tasks are futures themselves and can be seamlessly combined with other tasks or futures.

Task guarantees that:

  • the wrapped future is cancelled if the task is dropped.
  • the wrapped future is destroyed on the executor’s context it was submitted on.
  • the executor is kept alive for as long as the task itself is kept alive.

To create a task, use the ExecutorProtocol.trySpawn(...) method.

To cancel the task, invoke cancel() on it.