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
Over 2 million developers have joined DZone.
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
  • Getting Started With Agentic Workflows in Java and Quarkus

Trending

  • AI Paradigm Shift: Analytics Without SQL
  • Contract-First Integration: Building Scalable Systems With Flyway, OpenAPI, and Kafka
  • Stop Running Two Data Systems for One Agent Query
  • Run Gemma 4 on Your Laptop: A Hands-On Guide to Google's Latest Open Multimodal LLM
  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 user avatar
Grzegorz Piwowarek
·
Aug. 23, 18 · Tutorial
Likes (25)
Comment
Save
Tweet
Share
44.8K Views

Join the DZone community and get the full member experience.

Join For Free

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
  • Getting Started With Agentic Workflows in Java and Quarkus

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • 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