Fire And Forget Task (ARCH4021)
Esta página aún no está disponible en tu idioma.
Key java.async.ignored-future · Severity warning · Category async
When a CompletableFuture or task is produced as a bare expression statement and never awaited, assigned, or returned, its exceptions are lost and its ordering is undefined. The result is never observed.
Incorrect
Section titled “Incorrect”void process(List<Order> orders) { for (Order o : orders) { CompletableFuture.runAsync(() -> save(o)); // result ignored }}Correct
Section titled “Correct”void process(List<Order> orders) { List<CompletableFuture<Void>> futures = new ArrayList<>(); for (Order o : orders) { futures.add(CompletableFuture.runAsync(() -> save(o))); } CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).join();}Assign the produced future and wait on it with join()/get(), or return it to the caller.