3

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
3
  • 2
    My guess is that, since Duration was just introduced in Java 8, they either didn't think of it, or decided it was "too new" and stuck with a more traditional and usual implementation. Commented Oct 17, 2020 at 18:23
  • 3
    I would guess for the reason @user949300 said, and also much of the rest of the Java concurrency API uses TimeUnit (because Duration didn't exist at that point) so this way it's consistent. Commented Oct 17, 2020 at 21:30
  • 1
    I feel that overloaded methods with both styles would have been better. That is how you slowly push the users towards newer (and objectively better) API. Commented Oct 19, 2020 at 5:51

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.