nerdexam
Oracle

1Z0-805 · Question #14

Given: class Fibonacci extends RecursiveTask<Integer> { final int n; Fibonacci (int n) { this.n = n } Integer compute () { if (n <= 1) return n; Fibonacci f1 = new Fibonacci (n ?1); Oracle 1Z0-805…

The correct answer is B. The program produces the correct result, with performance degraded to the equivalent of being. Changing the code is not useful. In the original code (return f2.compute() + f1.join; ) f1 and f2 are run in parallel. The result is joined. With the changed code (return f1.join() + f2.compute();) f1 is first executed and finished, then is f2 Note 1:The join method allows one…

Concurrency

Question

Given:

class Fibonacci extends RecursiveTask<Integer> { final int n; Fibonacci (int n) { this.n = n } Integer compute () { if (n <= 1) return n; Fibonacci f1 = new Fibonacci (n ?1); Oracle 1Z0-805 Exam f1.fork; Fibonacci f2 = new Fibonacci (n ?2); return f2.compute() + f1.join; // Line ** } } Assume that line ** is replaced with:

return f1.join() + f2.compute(); // Line ** What is the likely result?

Options

  • AThe program produces the correct result, with similar performance to the original.
  • BThe program produces the correct result, with performance degraded to the equivalent of being
  • CThe program produces an incorrect result.
  • DThe program goes into an infinite loop.
  • EAn exception is thrown at runtime.
  • FThe program produces the correct result, with better performance than the original.

How the community answered

(25 responses)
  • A
    12% (3)
  • B
    76% (19)
  • D
    4% (1)
  • E
    4% (1)
  • F
    4% (1)

Explanation

Changing the code is not useful. In the original code (return f2.compute() + f1.join; ) f1 and f2 are run in parallel. The result is joined. With the changed code (return f1.join() + f2.compute();) f1 is first executed and finished, then is f2 Note 1:The join method allows one thread to wait for the completion of another. If t is a Thread object whose thread is currently executing, t.join(); causes the current thread to pause execution until t's thread terminates. Note 2:New in the Java SE 7 release, the fork/join framework is an implementation of the ExecutorService interface that helps you take advantage of multiple processors. It is designed for work that can be broken into smaller pieces recursively. The goal is to use all the available processing power to enhance the performance of your application. As with any ExecutorService, the fork/join framework distributes tasks to worker threads in a thread pool. The fork/join framework is distinct because it uses a work-stealing algorithm. Worker threads that run out of things to do can steal tasks from other threads that are still busy.

Topics

#Fork/Join framework#RecursiveTask#fork/join ordering#parallel performance

Community Discussion

No community discussion yet for this question.

Full 1Z0-805 Practice