Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Challenge ForkJoinPool | Multithreading Best Practices
Multithreading in Java
course content

Course Content

Multithreading in Java

Multithreading in Java

1. Multithreading Basics
2. Synchronized Collections
3. High-level Synchronization Mechanisms
4. Multithreading Best Practices

Challenge ForkJoinPool

Task

Imagine you're organizing a large charity event and need to tally up all the donations received. You have a list of donation amounts and want to find out the total sum of all donations. To make this task more manageable, you decide to divide the list into smaller chunks and distribute these chunks among your friends to help with the counting. Each friend calculates the total amount for their assigned chunk and reports the result back to you. You then combine all these results to get the final total.

You have a DonationTask class that extends RecursiveTask<Long>. You need to override the compute() method in this class to implement the logic for splitting the donation list and calculating the amounts.

The DonationTask class includes a constant THRESHOLD field set to 200. This threshold specifies the maximum number of donations that a single chunk should contain. Do not change this threshold.

For example, if you start with a list of 1,000 donations, you should break it into chunks of 200 or fewer donations each. Then, calculate the total amount by adding up the results from each chunk.

  • THRESHOLD - threshold of array splitting;
  • long[] listDonations - source array with all donations;
  • int start - the beginning of the array;
  • int end - end of the array.

There is also a RunnableTask class where we run the program. This is where we initialize our list and execute it in a ForkJoinPool. After everything is up and running, we display the result.

If you implement the compute() method correctly, you should get the total amount:

Solution hints

In the compute() method, we need to check if the size of our array (donation list) is smaller than the current threshold, THRESHOLD. If it is smaller, then great; we simply loop through all the elements of this array (donation list), sum them up, and return the result.

If the size is larger, then we need to find the middle of the array and recursively process the left and right sides of the array (as if we’re dividing the list among friends).

To do this, we create 2 DonationTask objects, pass the array boundaries and the array itself as parameters, then call the fork() method on these objects. Finally, we get the sum result by calling join() on each task, adding the results together, and returning the total.

Note

It sounds quite complicated, but in fact you just need to understand and implement the illustration above!

I'm sure you can do it! But if you have difficulties, you can see the solution.

Once you are sure that everything is working, run the verification tests on the path /src/test/java/TaskForkJoinTest.java.

Everything was clear?

Section 4. Chapter 3
We're sorry to hear that something went wrong. What happened?
some-alt