1Z0-805 · Question #56
Given the following incorrect program: class MyTask extends RecursiveTask<Integer> { final int low; final int high; static final int THRESHOLD = / . . . / MyTask (int low, int high) { this.low =…
The correct answer is A. Results must be retrieved from the newly created MyTask Instances and combined. D. The compute () method must be changed to return an Integer result. D: the compute() method must return a result. A: These results must be combined (in the lineinvokeAll(new MyTask(low, mid), new MyTask(mid, high));) Note 1:A RecursiveTask is a recursive result-bearing ForkJoinTask. Note 2: The invokeAll(ForkJoinTask<?>... tasks) forks the…
Question
Given the following incorrect program:
class MyTask extends RecursiveTask<Integer> { final int low; final int high; static final int THRESHOLD = /* . . . / MyTask (int low, int high) { this.low = low; this.high = high; } Integer computeDirectly()/ . . . */ protected void compute() { if (high ?low<;= THRESHOLD) return computeDirectly(); int mid = (low + high) / 2; invokeAll(new MyTask(low, mid), new MyTask(mid, high)); Which two changes make the program work correctly?
Options
- AResults must be retrieved from the newly created MyTask Instances and combined.
- BThe THRESHOLD value must be increased so that the overhead of task creation does not dominate
- CThe midpoint computation must be altered so that it splits the workload in an optimal manner.
- DThe compute () method must be changed to return an Integer result.
- EThe computeDirectly () method must be enhanced to fork () newly created tasks.
- FThe MyTask class must be modified to extend RecursiveAction instead of RecursiveTask.
How the community answered
(23 responses)- A83% (19)
- B9% (2)
- E4% (1)
- F4% (1)
Explanation
D: the compute() method must return a result. A: These results must be combined (in the lineinvokeAll(new MyTask(low, mid), new MyTask(mid, high));) Note 1:A RecursiveTask is a recursive result-bearing ForkJoinTask. Note 2: The invokeAll(ForkJoinTask<?>... tasks) forks the given tasks, returning when isDone holds for each task or an (unchecked) exception is encountered, in which case the exception is Note 3: Using the fork/join framework is simple. The first step is to write some code that performs a segment of the work. Your code should look similar to this: if (my portion of the work is small enough) do the work directly split my work into two pieces invoke the two pieces and wait for the results Wrap this code as a ForkJoinTask subclass, typically as one of its more specialized types RecursiveTask(which can return a result) or RecursiveAction. Oracle 1Z0-805 Exam
Topics
Community Discussion
No community discussion yet for this question.