Aller au contenu

Sync Over Async (ARCH4023)

Ce contenu n’est pas encore disponible dans votre langue.

Key java.async.blocking-future-get · Severity error · Category async

Blocking on an asynchronous operation with get()/join() from a synchronous context risks deadlock and thread-pool starvation, and defeats the purpose of going async in the first place.

public Report build() {
CompletableFuture<Data> f = fetchAsync();
Data data = f.get(); // synchronous block
return render(data);
}
public CompletableFuture<Report> build() {
return fetchAsync()
.thenApply(this::render); // chain, don't block
}

Replace the blocking get()/join() with thenApply/thenCompose to chain asynchronously and return the future.