...
Code Block | ||
---|---|---|
| ||
try (ExecutorService executor = Executors.newVirtualThreadExecutor()) { // Submits a value-returning task and waits for the result Future<String> future = executor.submit(() -> "foo"); String result = future.join(); // Submits two value-returning tasks to get a Stream that is lazily populated // with completed Future objects as the tasks complete Stream<Future<String>> stream = executor.submit(List.of(() -> "foo", () -> "bar")); stream.filter(Future::isCompletedNormally) .map(Future::join) .forEach(System.out::println); // Executes two value-returning tasks, waiting for both to complete List<Future<String>> results1 = executor.invokeAll(List.of(() -> "foo", () -> "bar")); // Executes two value-returning tasks, waiting for both to complete. If one of the // tasks completes with an exception, the other is cancelled. List<Future<String>> results2 = executor.invokeAll(List.of(() -> "foo", () -> "bar"), /*cancelOnExceptionwaitAll*/ truefalse); // Executes two value-returning tasks, returning the result of the first to // complete, cancelling the other. String first = executor.invokeAny(List.of(() -> "foo", () -> "bar")); } |
...
Overview
Content Tools
ThemeBuilder