2

I need some help on the literature used in the anyOf javadoc in CompletableFuture.

static CompletableFuture<Object> anyOf(CompletableFuture<?>... cfs) Returns a new CompletableFuture that is completed when any of the given CompletableFutures complete, with the same result.

What does this 'same result' mean? is same as what? same as any other future in the same array of CompletableFuture?

Thanks Venkatesh Laguduva

Venkatesh Laguduva
  • 13,448
  • 6
  • 33
  • 45

2 Answers2

3

When it says the same result, it means the same result as the first completed future.

If you use something like:

CompletableFuture<String> stringFuture = CompletableFuture.supplyAsync(() -> {
    Thread.sleep(3000); // handle exc
    return "String";
});

CompletableFuture<Integer> intFuture = CompletableFuture.supplyAsync(() -> {
    Thread.sleep(4000); // handle exc
    return 1;
});

The result will be the same as the first completed future, if the string completes first, the object will be of string type with that future's value, and so on.

In this case:

CompletableFuture.anyOf(stringFuture, intFuture).get()

Would return "string", because it completes first, if the intFuture completes first it would return 1;

Jose Da Silva Gomes
  • 3,814
  • 3
  • 24
  • 34
  • >>same result as the first completed future. Gotcha! was missing this line; is there any way we can get the first future completed successfully? – Venkatesh Laguduva Mar 27 '18 at 16:46
  • 1
    When you use `anyOf` you receive another `CompletableFuture`, if you execute an operation that waits for the result like `get()` you should get the first future result. (in the example i use `get()`, you can use another of the provided methods and handle the exception). – Jose Da Silva Gomes Mar 27 '18 at 16:49
  • 1
    But if you mean successfully without any completable futures with exceptions or failures you can check this https://stackoverflow.com/questions/33913193/completablefuture-waiting-for-first-one-normally-return – Jose Da Silva Gomes Mar 27 '18 at 16:51
2

same as any other future in the same array of CompletableFuture?

Indeed.

As soon as any CompletableFutures of the array var-args is completed, the method returns a new CompletableFuture object with the same result.

If you look into the implementation you can have the confirmation.

public static CompletableFuture<Object> anyOf(CompletableFuture<?>... cfs) {
    return orTree(cfs, 0, cfs.length - 1);
}

invokes orTree() :

static CompletableFuture<Object> orTree(CompletableFuture<?>[] cfs,
                                        int lo, int hi) {
    CompletableFuture<Object> d = new CompletableFuture<Object>();
    if (lo <= hi) {
        CompletableFuture<?> a, b;
        int mid = (lo + hi) >>> 1;
        if ((a = (lo == mid ? cfs[lo] :
                  orTree(cfs, lo, mid))) == null ||
            (b = (lo == hi ? a : (hi == mid+1) ? cfs[hi] :
                  orTree(cfs, mid+1, hi)))  == null)
            throw new NullPointerException();
        if (!d.orRelay(a, b)) {
            OrRelay<?,?> c = new OrRelay<>(d, a, b);
            a.orpush(b, c);
            c.tryFire(SYNC);
        }
    }
    return d;
}
davidxxx
  • 125,838
  • 23
  • 214
  • 215