Versions Compared

Key

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

...

This example runs three tasks and selects the result of the first task to complete. The remaining tasks are cancelled, which causes the virtual threads running it them to be interrupted.

Code Block
languagejava
try (ExecutorService executor = Executors.newUnboundedVirtualThreadExecutor()) {
    Callable<String> task1 = () -> "foo";
    Callable<String> task2 = () -> "bar";
    Callable<String> task3 = () -> "baz";
    String result = executor.invokeAny(List.of(task1, task2, task3));
}

...