3 Mayıs 2019 Cuma

CompletableFuture.anyOf metodu

Giriş
Açıklaması şöyle.
Completes when any of several CompletableFutures completes, with the same result.
Açıklaması şöyle
Waiting for any future to complete can be quickly done using  CompletableFuture#anyOf, and it handles exception propagation correctly out of the box.

However, there’s a slight complication.

If you look at the signature of  CompletableFuture#anyOf, you will see that it’s not very practical because it accepts multiple  CompletableFutures<?> and returns a single  CompletableFuture< Object> , but this is not the main issue here (just a slight inconvenience).

The real problem is that the  CompletableFuture<Object> returned by the method is not the future that completed first, but a new CompletableFuture instance that completes when any future completes.

This makes the whole idea of waiting for a future and then removing it from a list of remaining futures a bit complicated. We can’t rely on reference equality, so we can either do a linear scan after each signal from  CompletableFuture#anyOf, or try to come up with something better.

Hiç yorum yok:

Yorum Gönder