ForkJoinFramwork,直接调用 compute() 而无需显式 ForkJoinPool/ExecutorService
ForkJoinFramwork, call compute() directly without explicit ForkJoinPool/ExecutorService
问:直接调用扩展 RecursiveAction/Task 的 class“X”中的 compute() 方法会发生什么情况?
无需显式调用 ForkJoinPool,如下所示:
ForkJoinPool pool = new ForkJoinPool();
ForkJoinTask<?> task = new X(...);
pool.invoke(task);
相反,像下面这样的函数调用仍然会产生类似的结果:
// X extends RecursiveAction/Task<V>, which also means it is a subclass of ForkJoinTask.
X x = new X(...);
x.compute();
当调用class X 中的fork()/invokeAll() 方法时会发生什么(不存在显式ExecutorService)?
我的假设是,当调用扩展 class X 中的 fork() 或 invoke() 方法时,一个新任务会自动提交给 ForkJoinPool.commonPool (),如果没有明确指定池。但是我找不到任何指定此行为的文档。
(可能是相关的 oracle 文档的引用)
A "main" ForkJoinTask begins execution when it is explicitly submitted
to a ForkJoinPool, or, if not already engaged in a ForkJoin
computation, commenced in the ForkJoinPool.commonPool() via fork(),
invoke(), or related methods.
如有任何信息或关键字,我们将不胜感激。
一段代码(注意“othertask.fork()”):
class X extends RecursiveTask<Double>{
private Double[] numbersToBeOperated;
private int start;
private int end;
public X(Double numbersToBeOperated, int start, int end){
// define the fields, i.e., this.* = *;
}
@Override
protected Double compute(){
if(taskDividedToBaseCase){
// do works
} else {
int middle = start + ((end - start) / 2);
RecursiveTask<Double> otherTask = new X(numbersToBeOperated, start, middle);
otherTask.fork(); // what happens here, when compute() is directly called?
return new X(numbersToBeOperated, middle, end).compute() + otherTask.join();
// or invokeAll(new X(...), new X(...)); if RecursiveAction
}
}
}
// then instantiate X and call X.compute() directly.
引用 java.util.concurrent.ForkJoinTask<V>.fork()
:
Arranges to asynchronously execute this task in the pool the current task is running in, if applicable, or using the
ForkJoinPool.commonPool() if not inForkJoinPool().
它会转到 commonPool()。
也感谢@Holger 指向此文档。
问:直接调用扩展 RecursiveAction/Task 的 class“X”中的 compute() 方法会发生什么情况?
无需显式调用 ForkJoinPool,如下所示:
ForkJoinPool pool = new ForkJoinPool();
ForkJoinTask<?> task = new X(...);
pool.invoke(task);
相反,像下面这样的函数调用仍然会产生类似的结果:
// X extends RecursiveAction/Task<V>, which also means it is a subclass of ForkJoinTask.
X x = new X(...);
x.compute();
当调用class X 中的fork()/invokeAll() 方法时会发生什么(不存在显式ExecutorService)?
我的假设是,当调用扩展 class X 中的 fork() 或 invoke() 方法时,一个新任务会自动提交给 ForkJoinPool.commonPool (),如果没有明确指定池。但是我找不到任何指定此行为的文档。
(可能是相关的 oracle 文档的引用)
A "main" ForkJoinTask begins execution when it is explicitly submitted to a ForkJoinPool, or, if not already engaged in a ForkJoin computation, commenced in the ForkJoinPool.commonPool() via fork(), invoke(), or related methods.
如有任何信息或关键字,我们将不胜感激。
一段代码(注意“othertask.fork()”):
class X extends RecursiveTask<Double>{
private Double[] numbersToBeOperated;
private int start;
private int end;
public X(Double numbersToBeOperated, int start, int end){
// define the fields, i.e., this.* = *;
}
@Override
protected Double compute(){
if(taskDividedToBaseCase){
// do works
} else {
int middle = start + ((end - start) / 2);
RecursiveTask<Double> otherTask = new X(numbersToBeOperated, start, middle);
otherTask.fork(); // what happens here, when compute() is directly called?
return new X(numbersToBeOperated, middle, end).compute() + otherTask.join();
// or invokeAll(new X(...), new X(...)); if RecursiveAction
}
}
}
// then instantiate X and call X.compute() directly.
引用 java.util.concurrent.ForkJoinTask<V>.fork()
:
Arranges to asynchronously execute this task in the pool the current task is running in, if applicable, or using the ForkJoinPool.commonPool() if not inForkJoinPool().
它会转到 commonPool()。
也感谢@Holger 指向此文档。