Q:

Executing a method in Java after either prior asynchronous methods are done?

I'm trying to use Completetable future to run two task in async. The program runs asynchronously, such that a() and b() run first, in any order, concurrently. But c() can only run after either one of a() or b() completes
class Pair{
  public void pair2() throws InterruptedException, ExecutionException {
    CompletableFuture<Void> fa = CompletableFuture.runAsync(() -> a());
    CompletableFuture<Void> fb = CompletableFuture.runAsync(() -> b());

    if(fa.isDone || fb.isDone){ //should not be if loop.
      c();
    }
    return;
  }

  public void a(){
    System.out.println("I'm a.");
    return;
  }
  public void b(){
    System.out.println("I'm b.");
    return; 
  }

  public void c(){
    System.out.println("I'm c, I cannot be the first!");
    return;
  }
}

I'm not familiar with the CompletableFuture API, is there a way to check if either of the task is done and call upon the next method C?

A:

You could use one of the xxxEither methods. For example:
CompletableFuture<Void> fc = fa.acceptEither(fb, v -> c());

Or you could use the anyOf method:
CompletableFuture.anyOf(fa, fb).thenRun(this::c);

