219. Combining newVirtualThreadPerTaskExecutor() and streams
Streams and newVirtualThreadPerTaskExecutor() is a handy combination. Here is an example that relies on IntStream to submit 10 simple tasks and collect the returned List of Future instances:
try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { List<Future<String>> futures = IntStream.range(0, 10) .mapToObj(i -> executor.submit(() -> { return Thread.currentThread().toString() + "(" + i + ")"; })).collect(toList()); // here we have the following snippet of code } Next, we wait for each Future to complete by calling the get() method:
futures.forEach(f -> { try { logger.info(f.get()); } catch (InterruptedException | ExecutionException ex) { // handle exception } }); Moreover, using stream pipelines is quite useful in combination with invokeAll(). For instance, the following stream...