- Loading...
...
This following example uses submitTasks to submit three value returning tasks. It uses the CompletableFuture.streamcompleted method to obtain a stream that is lazily populated as the tasks complete.
| Code Block | ||
|---|---|---|
| ||
try (ExecutorService executor = Executors.newVirtualThreadExecutor()) {
Callable<String> task1 = () -> "foo";
Callable<String> task2 = () -> "bar";
Callable<String> task3 = () -> "baz";
List<CompletableFuture<String>> cfs = executor.submitTasks(List.of(task1, task2, task3));
CompletableFuture.streamcomplete(cfs)
.map(CompletableFuture::join)
.forEach(System.out::println);
} |
...