Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

In this example, the main task will not complete until foo and bar complete. The foo method, executing in a virtual thread, does not complete until the task that is submitted completes. Similarly bar, executing in another virtual thread, does not complete until the task that it submitted completes.


Cancellation

...

and Deadlines

The early prototype of Project Loom has basic support for cancellation. Further exploration is needed but as virtual threads represented by  java.lang.Thread objects it means that they support thread interruption. In addition, if a thread blocked in ExecutorService::close is interrupted then it will attempt to stop all tasks/threads as if by invoking the exector's shutdownNow method. If all tasks are well behaves and terminate quickly when interrupted then it allow the executor to terminate quickly.

An ExecutorService can be wrapped

A FiberScope can be created with options that configure how cancellation is handled. At this time, the options are PROPAGATE_CANCEL, CANCEL_AT_CLOSE, and IGNORE_CANCEL.

The following example uses the PROPAGATE_CANCEL option:

Code Block
languagejava
     try (var scope = FiberScope.open(Option.PROPAGTE_CANCEL)) {
         var fiber1 = scope.schedule(task);
         var fiber2 = scope.schedule(task);
     }

If a fiber executing in this scope is cancelled then it will also cancel fiber1 and fiber2.

The IGNORE_CANCEL option is for recovery/cleanup tasks that cannot be cancelled. The Fiber.cancelled() method always returns false when executing in scope created with this option.

Deadlines

An ExecutorService can be created with a deadline so that its shutdowNow method is invoked if the deadline expires before the executor has terminated.

...

Deadlines do not correctly work with nested usages. In the original FiberScope prototype, the scope had a owner that would be interrupted when the deadline expired. We may explored something similar here.