Java 9 introduced many new methods in the CompletableFuture API that support execution timeouts. For example:
public CompletableFuture<T> orTimeout(long timeout, TimeUnit unit);
public CompletableFuture<T> completeOnTimeout(T value, long timeout, TimeUnit unit);
More can be found in the documentation.
These methods take a long
and a TimeUnit
as arguments. Is there a specific reason it has been done so? Wouldn't Duration
be a better choice here?
public CompletableFuture<T> orTimeout(Duration duration);
public CompletableFuture<T> completeOnTimeout(T value, Duration duration);
Glorfindel
3,1676 gold badges28 silver badges34 bronze badges
asked Oct 17, 2020 at 16:17
lang-java
TimeUnit
(becauseDuration
didn't exist at that point) so this way it's consistent.