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

  • Proper Java Exception Handling
  • Java Virtual Threads and Scaling
  • Java’s Next Act: Native Speed for a Cloud-Native World
  • The Energy Efficiency of JVMs and the Role of GraalVM

Trending

  • Agile and Quality Engineering: A Holistic Perspective
  • Apache Doris vs Elasticsearch: An In-Depth Comparative Analysis
  • A Guide to Developing Large Language Models Part 1: Pretraining
  • Breaking Bottlenecks: Applying the Theory of Constraints to Software Development
  1. DZone
  2. Coding
  3. Java
  4. Problems With Nested CompletableFuture in Java

Problems With Nested CompletableFuture in Java

This blog describes problems that can occur using CompletableFuture of Java in a nested fashion and solutions for how to fix these problems.

By 
Harish Kumar Murugesan user avatar
Harish Kumar Murugesan
·
Feb. 26, 24 · Code Snippet
Likes (4)
Comment
Save
Tweet
Share
7.3K Views

Join the DZone community and get the full member experience.

Join For Free

CompletableFuture was introduced in Java 8 for executing things in an asynchronous way. This can help to prevent the main thread from waiting to complete the execution of a particular block of code that is not needed for the main thread execution. CompletableFuture uses threads from ForkJoinPool.commonPool() for its execution, if there is no custom executor passed as an argument.

If the parallelism is less than two, then CompletableFuture creates a new thread for each asynchronous task submitted. ForkJoinPool.commonPool() will have the number of threads based on the java.util.concurrent.ForkJoinPool.common.parallelism property set.

java.util.concurrent.ForkJoinPool.common.parallelism is set to default with a value equal to the number of cores available in the system where the JVM is running. For example, if we are running the JVM on a 4 CPU machine, then the value of java.util.concurrent.ForkJoinPool.common.parallelism will be 4, and ForkJoinPool.commonPool() will also have the 4 threads created. We can increase the value of this java.util.concurrent.ForkJoinPool.common.parallelism by passing this parameter at startup via —Djava.util.concurrent.ForkJoinPool.common.parallelism=<count>. This property can’t be modified at runtime and must be set during the JVM startup as a command line argument.

Nested CompletableFuture in the code will create problems when the application gets a high volume of requests to execute it. For example, in the code, there is one CompletableFuture1, which in turn contains another CompletableFuture2. Both the CompletableFuture 1 and 2 require separate threads to be allocated to execute the task assigned. In this case, it requires two threads from them ForkJoinPool.commonPool() to execute the tasks. It will run successfully if there are no other requests in the application while this code is executed, and the parallelism is 4. It will get the threads available in ForkJoinPool.commonPool() as they are available, and the tasks will be executed successfully. If there are more requests coming to the application, then more threads are needed to execute these nested CompletableFuture tasks. The ForkJoinPool.commonPool() may run out of threads, and the application may get into a hung kind of situation without being able to process the requests.

For example, if we are running the application on a machine with a CPU count of 4, then there will be 4 threads in the ForkJoinPool.commonPool() by default. If the application receives 4 requests simultaneously, then it will enter CompletableFuture1, and it will try to execute CompletableFuture2, where it will wait for the threads as all the ForkJoinPool.commonPool() threads were already busy executing CompletableFuture1. This will make the application go into a kind of hung state, and it won’t proceed with processing the current 4 requests as well as the requests that are going to reach this nested CompletableFuture code. It might also halt the other requests that use CompletableFuture inside it from processing. 

To resolve this situation, the java.util.concurrent.ForkJoinPool.common.parallelism parameter can be increased to a higher value as a short-term workaround. That may resolve the situation mentioned above in the blog, but it will occur if there is a number of simultaneous requests that are equal to or more than the java.util.concurrent.ForkJoinPool.common.parallelism parameter value set. Also, sometimes, the issue may not occur even if there is a variation of milliseconds while receiving the simultaneous requests or if the CompletableFuture code block is getting executed in a faster manner. As a permanent fix to this situation, it is always better to avoid having nested asynchronous tasks like this as it has the overhead of using more threads as well as a possibility of getting into a hung situation.

Java virtual machine Java (programming language) Error code Error message

Opinions expressed by DZone contributors are their own.

Related

  • Proper Java Exception Handling
  • Java Virtual Threads and Scaling
  • Java’s Next Act: Native Speed for a Cloud-Native World
  • The Energy Efficiency of JVMs and the Role of GraalVM

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!