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 Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
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
  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!

Grzegorz Piwowarek user avatar by
Grzegorz Piwowarek
·
Aug. 23, 18 · Tutorial
Like (24)
Save
Tweet
Share
37.11K 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.

Popular on DZone

  • Real-Time Stream Processing With Hazelcast and StreamNative
  • Microservices Discovery With Eureka
  • Express Hibernate Queries as Type-Safe Java Streams
  • API Design Patterns Review

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: