Chapter TWENTY-EIGHT
Fork/Join Framework


Exam Objectives

Use parallel Fork/Join Framework.

Answers

1. The correct answer is B.
Option A is false. RecursiveAction is a subclass of ForkJoinTask.
Option B is true. By default, one thread per CPU is created.
Option C is false. You don't need to shut down a ForkJoinPool explicitly. It's closed when the program ends.
Option D is false. fork() doesn't block the program, just add a task to the thread's queue.


2. The correct answer is B.
If you don't call fork() before join(), there won't be any result to get.
If you call join() before compute(), the program will perform like if it was executed in one thread because of the blocking produced by join().


3. The correct answer is D.
ForkJoinTask.invoke() returns the same type as the generic type of RecursiveTask is the only true statement. You can't use invokeAll() because this method doesn't return a result. You can't use an ExecutorService because it uses a different type of threads and you can't have an action triggered when the task is completed.


4. The correct answer is C.
The program tries to get the value of the nth Fibonacci number with the Fork/Join framework, but it does it in a very inefficient way because the subtasks will be very small.
Remember that Fork/Join is not the best choice for everything. Actually, solving this problem using this recursive algorithm is not the best way, but implementing it with an algorithm where Karatsuba multiplication and parallelism can be used will certainly provide much better results.