Efficient Asynchronous Processing Using CyclicBarrier and CompletableFuture in Java
Learn how JAVA concurrency features such as CyclicBarrier and CompletableFuture can be used together to achieve efficiency.
Join the DZone community and get the full member experience.
Join For FreeIn today’s world enterprise applications increasingly require the ability to asynchronously process large datasets. The processing of data must correlate and compute results at the same time. This article illustrates how CyclicBarrier and CompletableFuture, combined, perform efficiently in processing and producing desired results from large datasets.
Why to Use Asynchronous Processing?
Asynchronous processing lets tasks run without blocking others. Unlike synchronous processing, which runs tasks in order, it allows multiple tasks to proceed at once. This method is handy for tasks that need to wait for external resources, like network requests. It boosts efficiency and responsiveness in applications.
In this article, Asynchronous processing of a given dataset is ensured using the following features:
- CyclicBarrier: A synchronizer that allows a set of threads to wait for each other to reach the same barrier point before continuing, which means no thread can pass beyond a point (the barrier) unless all threads are at the same barrier.
- CompletableFuture: Java provides a multifaceted class that helps in asynchronous computation. It is a powerful tool for nonblocking computation, enhancing the performance of applications. In our scenario, it allows us to run a parallel average salary calculation for each department.
Problem Statement
Let's consider that there is a comma-separated file (CSV) that contains a list of employees, their departments, and their salaries. The objective is to find the average salary for each department using asynchronous execution; in other words, execute code in parallel and reduce execution time.
The dataset shown below is available at the GitHub location.
Solution Approach
The solution is split into the following steps:
- Read CSV file and parse employee data: The OpenCV library is used to read and parse the CSV file into a list of Employee Java objects.
- Group employees by department: Using Java’s Collectors.groupingBy, we will group employees by their department.
- Calculate average salaries asynchronously: Using CompletableFuture, the average salary for each department is calculated concurrently.
- Synchronize the results: With CyclicBarrier, it is ensured that all the department calculations are completed before the results are generated.
The code snippet is as follows:
Explanation of Code
- Data loading: The opencsv library is used to parse the employees.csv file into a list of employees. This allows us to easily work with employee data in our code.
- Grouping by department: The employees are grouped by their department field using the static factory method
Collectors.groupingBy()
[made available since Java8] allows the processing of collections of data in a declarative way. - Asynchronous execution: Each department's salary calculation is done asynchronously using
CompletableFuture.runAsync
. This ensures that the calculations are done in parallel, leveraging available CPU resources efficiently. - Synchronization with CyclicBarrier: The CyclicBarrier ensures that all department calculations are completed before generating the end results. The
barrier.await()
method makes sure each thread waits for the others to reach this point before proceeding. - Calculating and storing averages: The
calculateAndStoreAverage
method computes the average salary for each department and stores it in a ConcurrentHashMap for thread-safe access. - Result: After all threads have completed, the average salary for each department is printed to the console as below.
Conclusion
Asynchronous Programming
CompletableFuture allows the execution of tasks concurrently, making it possible to calculate department averages in parallel.
Efficient Synchronization
The CyclicBarrier ensures that the program waits until all departments have completed their average salary calculations.
Java Concurrency
This is a simple and efficient concurrency solution by combining CompletableFuture and CyclicBarrier, which makes this approach perfect for multiple independent tasks that need to be run in parallel.
It is quite an efficient and scalable way to expand and manage a high number of departments or employee records without any major change in core logic. Therefore, following these asynchronous programming patterns, Java developers can now develop high-performance applications that process data in parallel within a single thread.
The full code snippet is available in my GitHub repository.
References
The article is based on various official resources. The official JAVA SE 17 documentations for CyclicBarrier and CompletableFuture are very insightful regarding functionality and their implementation.
Baeldung's guide on CyclicBarrier offers practical examples and use cases to show how to use cyclicBarrier for concurrent programming. Together, these references provide the background for the ideas and examples given in this article.
Opinions expressed by DZone contributors are their own.
Comments