Pri*_*imk 11 java fork-join threadpool forkjoinpool
ForkJoinPool的异步模式是什么意思?Javadoc提到它使队列(它是每线程队列吗?)FIFO而不是LIFO.这在实践中意味着什么?
glt*_*lts 11
a中的每个工作线程ForkJoinPool都有自己的工作队列.异步模式 涉及每个工作者从未从其工作队列加入的分叉任务的顺序.
工人在一个ForkJoinPool在FIFO异步模式处理这样的任务(先入先出)次序.默认情况下,ForkJoinPools以LIFO(后进先出)顺序处理此类任务.
重要的是要强调异步模式设置仅涉及从未加入的分叉任务.当使用ForkJoinPool它最初设计的时候,即递归fork/join任务分解时,asyncMode根本不会发挥作用.只有当一个worker没有参与实际的fork/join处理时,它才会执行异步任务,然后才会asyncMode查询该标志.
这是一个小程序,演示了两种不同的异步模式设置之间的区别:
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
/**
* Demo of {@code ForkJoinPool} behaviour in async and non-async mode.
*/
public class ForkJoinAsyncMode {
public static void main(String[] args) {
// Set the asyncMode argument below to true or false as desired:
ForkJoinPool pool = new ForkJoinPool(
4, ForkJoinPool.defaultForkJoinWorkerThreadFactory, null, true);
pool.invoke(new RecursiveRangeAction(0, 200));
pool.awaitQuiescence(2L, TimeUnit.SECONDS);
}
/**
* A {@code ForkJoinTask} that prints a range if the range is smaller than a
* certain threshold; otherwise halves the range and proceeds recursively.
* Every recursive invocation also forks off a task that is never joined.
*/
private static class RecursiveRangeAction extends RecursiveAction {
private static final AtomicInteger ASYNC_TASK_ID = new AtomicInteger();
private final int start;
private final int end;
RecursiveRangeAction(int start, int end) {
this.start = start;
this.end = end;
}
@Override
protected void compute() {
if (end - start < 10) {
System.out.format("%s range [%d-%d] done%n",
Thread.currentThread().getName(), start, end);
} else {
int mid = (start + end) >>> 1;
int id = ASYNC_TASK_ID.incrementAndGet();
System.out.format(
"%1$s [%2$d-%3$d] -< [%3$d-%4$d], fork async task %5$d%n",
Thread.currentThread().getName(), start, mid, end, id);
// Fork off additional asynchronous task that is never joined.
ForkJoinTask.adapt(() -> {
System.out.format("%s async task %d done%n",
Thread.currentThread().getName(), id);
}).fork();
invokeAll(new RecursiveRangeAction(start, mid),
new RecursiveRangeAction(mid, end));
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
在非异步模式(默认设置ForkJoinPool)中,从未加入的分叉任务以LIFO顺序执行.
当您以非异步模式运行示例程序时,查看一个工作程序的输出,您可能会看到如下模式:
ForkJoinPool-1-worker-0 [175-187] -< [187-200], fork async task 10
ForkJoinPool-1-worker-0 [175-181] -< [181-187], fork async task 11
ForkJoinPool-1-worker-0 range [175-181] done
ForkJoinPool-1-worker-0 range [181-187] done
ForkJoinPool-1-worker-0 [187-193] -< [193-200], fork async task 12
ForkJoinPool-1-worker-0 range [187-193] done
ForkJoinPool-1-worker-0 range [193-200] done
ForkJoinPool-1-worker-0 async task 12 done
ForkJoinPool-1-worker-0 async task 11 done
ForkJoinPool-1-worker-0 async task 10 done
Run Code Online (Sandbox Code Playgroud)
这里,任务10,11,12被分叉,并且一旦工人开始执行它们,则以相反的顺序执行.
另一方面,在异步模式下,再次查看一个worker的输出,该模式看起来如下所示:
ForkJoinPool-1-worker-3 [150-175] -< [175-200], fork async task 8
ForkJoinPool-1-worker-3 [150-162] -< [162-175], fork async task 9
ForkJoinPool-1-worker-3 [150-156] -< [156-162], fork async task 10
ForkJoinPool-1-worker-3 range [150-156] done
ForkJoinPool-1-worker-3 range [156-162] done
ForkJoinPool-1-worker-3 [162-168] -< [168-175], fork async task 11
...
ForkJoinPool-1-worker-3 async task 8 done
ForkJoinPool-1-worker-3 async task 9 done
ForkJoinPool-1-worker-3 async task 10 done
ForkJoinPool-1-worker-3 async task 11 done
Run Code Online (Sandbox Code Playgroud)
任务8,9,10,11分叉,然后按照提交的顺序执行.
何时使用哪种模式?每当ForkJoinPool选择一个线程池来利用其工作窃取属性而不是递归fork/join任务处理时,异步模式可能是更自然的选择,因为任务按它们提交的顺序执行.
像异步事件驱动的框架CompletableFuture有时被称为从异步模式中获利.例如,在构造复杂的CompletableFuture回调链时,ForkJoinPool异步模式下的自定义执行程序可能比默认执行程序稍好一些.(虽然我不能说经验.)
| 归档时间: |
|
| 查看次数: |
4237 次 |
| 最近记录: |