Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

670
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!