Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

668
Visualizações
Java CompletableFuture: Trying to run two void methods as CompletableFuture and block until both complete

I am trying to run two void method1 and method2 async and then block the code until both methods are done.

Does this achieve that?

      CompletableFuture<Void> future1 = CompletableFuture.runAsync(() -> method1());
      CompletableFuture<Void> future2 = CompletableFuture.runAsync(() -> method2());
      CompletableFuture.allOf(future1, future2).join();
over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

I think code CompletableFuture.allOf(future1, future2).join(); should work fine. Could you try this, did you have any problems?

Or you can use something like:

  Arrays.asList(future1,future1).forEach(Future::get);
over 4 years ago · Santiago Trujillo Relatório

0

Why don't you use the dedicated method runAsync though?

   public static void main(String[] args) {
      CompletableFuture<Void> future1 = CompletableFuture.runAsync(() -> method1());
      CompletableFuture<Void> future2 = CompletableFuture.runAsync(() -> method2());

      CompletableFuture<Void> compound = CompletableFuture.allOf(future1, future2);
      compound.join();
      System.out.println("compound done");
}

And yes, your compound will be "joined" on the other thread (main in the example above), as such that compound done message will be printed only after both methods executed.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda