简体   繁体   English

关于fork-join框架

[英]About fork-join framework

Being the method fork(); 作为方法fork(); within compute() how come that does not get called another degree of parallelism each time the method compute() occurs? compute() ,每当方法compute()发生时,怎么会被称为另一个并行度? Is there a boolean flag perhaps? 是否有布尔标志? EDIT: 编辑:

overriding the method compute() of the class RecursiveTask: (pseudocode) 重写类的方法compute() RecursiveTask :(伪代码)

if {array.length<100)
do it
else
divide array by 2;
fork();
int righta = rightArray.compute();
int lefta =(Integer)leftArray.join();
return righta +lefta;

So basically this is the compute() method which gets called recursively and when fork() happens it makes it possible to use parallelism and process that task with another core. 所以基本上这是递归调用的compute()方法,当fork()发生时,它可以使用并行性并将该任务与另一个核心一起处理。 However being recursive fork() should be called all the times the method gets recursively called. 但是,在递归调用方法时,应始终调用递归的fork() So in the reality it does not happen (there would be no sense). 所以在现实中它不会发生(没有任何意义)。 Is it due to a boolean flag that says fork has already been activated? 是否由于布尔标志表示fork已被激活?

Thanks in advance. 提前致谢。

Look at the API 看看API

 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);
     f1.fork();
     Fibonacci f2 = new Fibonacci(n - 2);
     return f2.compute() + f1.join();
   }
 }

Each time compute() is called it will place another computation on another thread (or queue) via fork. 每次调用compute() ,它都会通过fork在另一个线程(或队列)上放置另一个计算。 compute continuously forks until there are no more n available to process. 连续计算,直到没有更多的n可供处理。 At this point compute will wait until the "right" side finishes while f1.join() waits for the "left" side to finish. 此时,计算将等到“右”侧完成,而f1.join()等待“左”侧完成。

Whenever join is invoked it will actually make the joining thread execute lower level tasks (lower on the binary tree) giving you the parallelism you want 每当调用join ,它实际上会使joining线程执行较低级别的任务(在二叉树上较低),为您提供所需的并行性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM