ForkJoin Framework Exercises
Table of Contents
This is a pair programming assignment. If you are on a team, this means that you and your partner should be doing the entirety of this assignment side-by-side, on a single computer, where one person is "driving" and the other is "navigating." Take turns every so often who is driving; you should each spend approximately 50% of the time driving.
Here are a few exercises to give you some practice with Java's ForkJoin Framework. You can use this ForkJoinRecursiveSum example as a starting point. You can also look here for another example.
Max value
(This may sound familiar…) Write a program called MaxValue.java
that finds
the maximum value in an array of ints using the ForkJoin framework. You may assume the array has at
least one element. You should use a sequential cutoff (e.g. 10), below which no
more recursive tasks are forked.
Longest Sequence
Consider the problem of finding the longest sequence of some number in an array
of numbers: longestSequence(i, arr)
returns the longest number of consecutive i
in arr
. For example, if arr
is {2, 17, 17, 8, 17, 17, 17, 0, 17, 1}
then
longestSequence(17, arr)
is 3
and longestSequence(9, arr)
is 0
. Write a
program called LongestSequence.java
that implements
longestSequence
using the ForkJoin framework. Do not employ a sequential
cut-off: your base case should process an array range containing one element. It
should contain a main method that tests it on a variety of cases. Document
clearly how your main method hits all the different sorts of cases that must be tested.
Hint: use something like this definition as the result type for your RecursiveTask.
class Result { int numLeftEdge; int numRightEdge; int numLongest; // ... other info if you think you need it }
For example, numLeftEdge
should represent the length of the sequence at the
beginning of the range processed by a subproblem. Think carefully about how to
combine results.
Longest Sequence with Cutoff
Copy and modify your program from the previous section to use a sequential
cutoff. Name it LongestSequenceCutoff.java
.