Interface Task

A task.
Tasks represent a thread of running code.
Tasks are cooperative and thus must yield in order to allow other tasks to run.
Tasks are created by calling task code. They can either terminate by themselves (gracefully or with an exception) or by being canceled from the outside with cancel.
If a task finishes with an exception it brings down the whole program.

Interface summary


current -> Task
Returns the current task.
group lambdas/List --required/int= -> Map
Runs the given lambdas as a group of concurrent tasks.

Statics

Returns the current task.

group lambdas/List --required/int=lambdas.size -> Map
Runs the given lambdas as a group of concurrent tasks.
Returns the results of running the lambdas as a Map keyed by the lambda index. If the lambda at a given index did not return, the results map will not contain and entry for it. The results map is insertion ordered, so it is possible to tell which lambda returned first.
If any of the lambdas throws an exception, the exception is propagated to the caller of Task.group which in return also throws.
If required is less than the number of lambdas, the method returns when required tasks have completed. If required is equal to 0, the method returns immediately.
Examples

results := Task.group [
  :: 42,
  :: 87,
]
print results  // => {0: 42, 1: 87}.

results = Task.group --required=1 [
  // Due to the required=1, this lambda will be aborted, once
  // the lambda returning 42 has finished.
  :: sleep --ms=1_000; 87,
  :: 42,
]
print results  // => { 1: 42 }.

Methods

cancel -> none
Cancels the task.
If the task has finally clauses, those are executed.
However, these must not yield, as the task won't run again. Use critical-do to
run code that must yield.

Returns the deadline for the task as a microsecond timestamp that can be compared against return values from Time.monotonic-us.
Returns null if the task has no deadline.

Whether this task is canceled.