DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • How to Identify the Underlying Causes of Connection Timeout Errors for MongoDB With Java
  • Spring Boot Timeout Handling With RestClient, WebClient, and RestTemplate
  • Coordinating Threads Using CountDownLatch
  • Stop Writing Database Migration Scripts: Declarative Schema Sync for Java

Trending

  • The Three Tiers of Agentic Incident Response: When to Trust AI Autonomy
  • Designing Frontend Applications for Failure, Not Just Success
  • RAG Architecture Patterns: Proxy, Caching, and Routing
  • The Permission Was Valid. The Action Was Not Authorized.

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. Java
  4. CompletableFuture Timeouts in Java

CompletableFuture Timeouts in Java

Futures and timeouts — seems like the start of a weird sci-fi movie. Let's take a look at how JDK 9 improves on JDK 8's CompletableFuture. Click here for more!

By 
Grzegorz Piwowarek
·
Aug. 23, 18 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
45.2K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

As much as I love Java 8's CompletableFuture, it has its downsides, specifically the idiomatic handling of timeouts.

Luckily, JDK 9 brought two new methods that provide the functionality developers longed for — the ExecutionException. This is crucial for ensuring the proper resiliency when working with asynchronous processing.

In this article, I will try to raise the awareness of the new crucial API methods.

Simply put, after calling the above method, CompletableFuture, the future will throw an ExecutionException as long as it doesn't complete within a specified timeout.

Take a look at this simple example:

CompletableFuture<Integer> future = CompletableFuture.supplyAsync(this::computeEndlessly)
  .orTimeout(1, TimeUnit.SECONDS);

future.get(); // java.util.concurrent.ExecutionException after waiting for 1 second


In this example, we can return a default value once the timeout is reached:

CompletableFuture<Integer> future = CompletableFuture.supplyAsync(this::computeEndlessly)
  .completeOnTimeout(42, 1, TimeUnit.SECONDS);

Integer result = future.get(); // 42


See? It is as simple as that. Although, I don't necessarily like the fact that we're always forced to precompute the default value. If you look at the signature, you will see that the value isn't computed lazily:

public CompletableFuture<T> completeOnTimeout(T value, long timeout, TimeUnit unit)


This could've been easily achieved, for example, by providing an alternative API method that accepted Supplier<T> instead of plain T. For more about this alternative API method, you can find more here.

The complete example can be found on GitHub.

Timeout (computing) Java (programming language)

Published at DZone with permission of Grzegorz Piwowarek. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • How to Identify the Underlying Causes of Connection Timeout Errors for MongoDB With Java
  • Spring Boot Timeout Handling With RestClient, WebClient, and RestTemplate
  • Coordinating Threads Using CountDownLatch
  • Stop Writing Database Migration Scripts: Declarative Schema Sync for Java

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook