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:
Sum: 500500