RecursiveTask如何用于计算斐波纳契数?

时间:2017-06-28 21:32:42

标签: recursion java-8 fork-join

我正在研究RecursiveTask计算Fibonacci数的经典例子。我添加了一些输出:请参阅http://jboston.net/2017/FibonacciOutp.txt,代码位于

之下

仍然无法理解这是如何工作的,为什么我们首先看到所有数字从12减少然后重复多次

number = 2 fcal1.join()= 1 fcal2.compute()= 0

number = 3 fcal1.join()= 1 fcal2.compute()= 1

代码:

import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveTask;

public class RecursiveTaskDemo {
public static void main(String[] args) {
    FibonacciCal fibonacciCal = new FibonacciCal(12);
    ForkJoinPool pool = new ForkJoinPool();
    int i = pool.invoke(fibonacciCal);
    System.out.println(i);
}
}

class FibonacciCal extends RecursiveTask<Integer> {
private static final long serialVersionUID = 1L;
final int num;

FibonacciCal(int num) {
    this.num = num;
}

@Override
protected Integer compute() {
    if (num <= 1) {
        return num;
    }
    System.out.println("number=" + num);
    FibonacciCal fcal1 = new FibonacciCal(num - 1);
    fcal1.fork();
    FibonacciCal fcal2 = new FibonacciCal(num - 2);
    int fcal1Join = fcal1.join();
    int fcal2Compute = fcal2.compute();
    System.out.println("number=" + num + " fcal1.join()=" + fcal1Join + " fcal2.compute()=" + fcal2Compute);
    return fcal2Compute + fcal1Join;
}

}

1 个答案:

答案 0 :(得分:2)

当调用FibonacciCal::compute时,它会分离一个线程来计算fib(n - 1)并在起始线程中计算fib(n - 2)。分支看起来有点像这样(fib(n)表示运行FibonacciCal(n).compute()的线程):

STARTING WITH pool.invoke(new FibonacciCal(5)):
fib(5)
A BIT LATER:
fib(5) === fib(3) // The fibcal2.compute() call, printing num = 3
       \== fib(4) // The fibcal1.fork() call, printing num = 4
LATER:
fib(5) === fib(3) === fib(1) // These fib(0/1)s are base cases and will start folding the tree back up
       |          \== fib(2) === fib(0) // Will return 1 and not fork
       |                     \== fib(1) // Will return 1 and not fork
       \== fib(4) === fib(2) === fib(0)
                  |          \== fib(1)
                  \== fib(3) === fib(1)
                             \== fib(2) === fib(0)
                                        \== fib(1)
METHODS START RETURNING:
fib(5) === fib(3) === 1
       |          \== fib(2) === 1
       |                     \== 1
       \== fib(4) === fib(2) === 1
                  |          \== 1
                  \== fib(3) === 1
                             \== fib(2) === 1
                                        \== 1
ADDITIONS START HAPPENING:
fib(5) === fib(3) === 1
       |          \== (1 + 1) = 2 // When a thread joins its child, it prints out its number again.
       |                          // Since the tree is now folding instead of unfolding, the printlns appear, approximately, the opposite order
       \== fib(4) === (1 + 1) = 2
                  \== fib(3) === 1
                             \== (1 + 1) = 2
LATER:
fib(5) === (1 + 2) = 3 === 1
       |               \== 2
       \== fib(4) === 2
                  \== (1 + 2) = 3 === 1
                                  \== 2
END:
8 === 3 === 1
  |     \== 2
  \== 5 === 2
        \== 3 === 1
              \== 2

你得到很多重复数字的原因是因为没有任何记忆。在这个包含fib(5)的示例中,您会看到fib(0)fib(1)有8个基本术语,fib(2)有3个术语,fib(3)有2个,{1}} {1}}。随着低阶条款开始加入他们的孩子,你会得到很多小fib(4)秒的printlns,直到结束,他们开始重新计算。