February 16, 2025

In right this moment’s world enterprise purposes more and more require the flexibility to asynchronously course of massive datasets. The processing of information should correlate and compute outcomes on the identical time. This text illustrates how CyclicBarrier and CompletableFuture, mixed, carry out effectively in processing and producing desired outcomes from massive datasets.

Why to Use Asynchronous Processing?

Asynchronous processing lets duties run with out blocking others. Not like synchronous processing, which runs duties so as, it permits a number of duties to proceed directly. This methodology is helpful for duties that want to attend for exterior assets, like community requests. It boosts effectivity and responsiveness in purposes. 

On this article, Asynchronous processing of a given dataset is ensured utilizing the next options:

  1. CyclicBarrier: A synchronizer that enables a set of threads to attend for one another to succeed in the identical barrier level earlier than persevering with, which suggests no thread can move past some extent (the barrier) until all threads are on the identical barrier.
  2. CompletableFuture: Java gives a multifaceted class that helps in asynchronous computation. It’s a highly effective instrument for nonblocking computation, enhancing the efficiency of purposes. In our state of affairs, it permits us to run a parallel common wage calculation for every division.

Drawback Assertion

Let’s take into account that there’s a comma-separated file (CSV) that accommodates a listing of staff, their departments, and their salaries. The target is to search out the common wage for every division utilizing asynchronous execution; in different phrases, execute code in parallel and scale back execution time.

The dataset proven beneath is accessible on the GitHub location.

Dataset

Resolution Strategy

The answer is cut up into the next steps:

  1. Learn CSV file and parse worker knowledge: The OpenCV library is used to learn and parse the CSV file into a listing of Worker Java objects.
  2. Group staff by division: Utilizing Java’s Collectors.groupingBy, we’ll group staff by their division.
  3. Calculate common salaries asynchronously: Utilizing CompletableFuture, the common wage for every division is calculated concurrently.
  4. Synchronize the outcomes: With CyclicBarrier, it’s ensured that every one the division calculations are accomplished earlier than the outcomes are generated.

The code snippet is as follows:

Code snippet

Rationalization of Code

  1. Information loading: The opencsv library is used to parse the staff.csv file into a listing of staff. This enables us to simply work with worker knowledge in our code.
  2. Grouping by division: The staff are grouped by their division area utilizing the static manufacturing unit methodology Collectors.groupingBy() [made available since Java8] permits the processing of collections of information in a declarative approach.
  3. Asynchronous execution: Every division’s wage calculation is finished asynchronously utilizing CompletableFuture.runAsync. This ensures that the calculations are accomplished in parallel, leveraging out there CPU assets effectively.
  4. Synchronization with CyclicBarrier: The CyclicBarrier ensures that every one division calculations are accomplished earlier than producing the top outcomes. The barrier.await() methodology makes certain every thread waits for the others to succeed in this level earlier than continuing.
  5. Calculating and storing averages: The calculateAndStoreAverage methodology computes the common wage for every division and shops it in a ConcurrentHashMap for thread-safe entry.
  6. Consequence: In spite of everything threads have accomplished, the common wage for every division is printed to the console as beneath.

The average salary for each department is printed to this console

Conclusion

Asynchronous Programming

CompletableFuture permits the execution of duties concurrently, making it potential to calculate division averages in parallel.

Environment friendly Synchronization

The CyclicBarrier ensures that this system waits till all departments have accomplished their common wage calculations.

Java Concurrency

This can be a easy and environment friendly concurrency answer by combining CompletableFuture and CyclicBarrier, which makes this strategy good for a number of unbiased duties that should be run in parallel.

It’s fairly an environment friendly and scalable technique to broaden and handle a excessive variety of departments or worker data with none main change in core logic. Due to this fact, following these asynchronous programming patterns, Java builders can now develop high-performance purposes that course of knowledge in parallel inside a single thread.

The complete code snippet is accessible in my GitHub repository.

References

The article is predicated on numerous official assets. The official JAVA SE 17 documentations for CyclicBarrier and CompletableFuture are very insightful relating to performance and their implementation. 

Baeldung’s information on CyclicBarrier affords sensible examples and use circumstances to indicate tips on how to use cyclicBarrier for concurrent programming. Collectively, these references present the background for the concepts and examples given on this article.