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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Spring Boot Timeout Handling With RestClient, WebClient, and RestTemplate
  • Coordinating Threads Using CountDownLatch
  • Using Python Libraries in Java
  • Designing a Java Connector for Software Integrations

Trending

  • AI-Based Threat Detection in Cloud Security
  • How to Practice TDD With Kotlin
  • Memory Leak Due to Time-Taking finalize() Method
  • Docker Model Runner: Streamlining AI Deployment for Developers
  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
43.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, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Spring Boot Timeout Handling With RestClient, WebClient, and RestTemplate
  • Coordinating Threads Using CountDownLatch
  • Using Python Libraries in Java
  • Designing a Java Connector for Software Integrations

Partner Resources

×

Comments
Oops! Something Went Wrong

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!