Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

344
Vistas
How to run multiple service calls in parallel using CompletableFuture?

I am returning following response to the user

class FinalResponseDTO {
    List<Service1ResponseDTO> service1ResponseDTO;
    Long totalCount;
    List<Service2ResponseDTO> service2ResponseDTO;
}

As of now I am making three sequential calls to compute this FinalResponseDTO, each of this call can be run independent of others. I tried making three different CompletableFuture as:

CompletableFuture<List<Service1ResponseDTO> future1 = CompletableFuture.supplyAsync(() -> service1.callMethod1());
CompletableFuture<Long> future2 = CompletableFuture.supplyAsync(() -> service2.callMethod2());
CompletableFuture<Service2ResponseDTO> future3 = CompletableFuture.supplyAsync(() -> service3.callMethod3());

If I do CompletableFuture.allOf(future1, future2, future3).join(); or Should I call CompletableFuture.allOf(future1, future2, future3).get();? Even If I call either of these join or get then how should I construct FinalResponseDTO from it. I am new to the Java 8 concurrency features such as CompletableFuture and I am confused, as each return type of each of these future is different, how should I get combined responses of all such futures and then construct my final output?

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

From the Javadocs of CompletableFuture.allOf():

Returns a new CompletableFuture that is completed when all of the given CompletableFutures complete. If any of the given CompletableFutures complete exceptionally, then the returned CompletableFuture also does so, with a CompletionException holding this exception as its cause. Otherwise, the results, if any, of the given CompletableFutures are not reflected in the returned CompletableFuture, but may be obtained by inspecting them individually.

So when the combining CompletableFuture completes, you can inspect the values and construct the final response object using a simple constructor by applying a function that constructs the object:

CompletableFuture.allOf(future1, future2, future3).thenApply(v -> 
    new FinalResponseDTO(future1.join(), future2.join(), future3.join())
);
over 4 years ago · Santiago Trujillo Denunciar

0

    CompletableFuture<List<Service1ResponseDTO> future1 = 
        CompletableFuture.supplyAsync(() -> service1.callMethod1());
    CompletableFuture<Long> future2 = 
        CompletableFuture.supplyAsync(() -> service2.callMethod2());
    CompletableFuture<List<Service2ResponseDTO>> future3 = 
        CompletableFuture.supplyAsync(() -> service3.callMethod3());

    CompletableFuture.allOf(future1, future2, future3).get();

    return new FinalResponseDTO(future1.join(), future2.join(), future3.join());

Note: supplyAsync runs on ForkJoinPool.commonPool() so it would be a good choice to provide your own executor e.g. Executors.newCachedThreadPool():

CompletableFuture.supplyAsync(() -> action, executor);
over 4 years ago · Santiago Trujillo Denunciar

0

Instead of using CompletableFuture.allOf, you could fall back to the somewhat hidden Applicative of the CompletableFuture:

static <T, R> CompletableFuture<R> alsoApply(CompletableFuture<T> future,     CompletableFuture<Function<T, R>> f) {
    return f.thenCompose(future::thenApply);
}

With this helper function you can execute futures in parallel threads:

CompletableFuture<String> future = alsoApply(
    CompletableFuture.supplyAsync(() -> "a"),
    CompletableFuture.supplyAsync(() -> "b")
.thenApply(b -> a -> a + b));

assertEquals("ab", future.get());

See this question and answer for where this is coming from and why and how it works.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda