- Loading...
...
This following example uses submitTasksthe submit method to submit three value returning tasks. It uses the CompletableFuture.completed method to obtain a stream that The stream 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.submitTaskssubmit(List.of(task1, task2, task3));
CompletableFuture.completed(cfs)
.map(CompletableFuture::join)
.forEach(System.out::println);
} |
...