什么是ForkJoinPool异步模式

时间:2011-04-12 18:42:12

标签: java threadpool fork-join forkjoinpool

ForkJoinPool的异步模式是什么意思? Javadoc提到它使队列(它是每线程队列吗?)FIFO而不是LIFO。这在实践中意味着什么?

2 个答案:

答案 0 :(得分:11)

ForkJoinPool中的每个工作线程都有自己的工作队列。 Async mode 涉及每个工作人员从未从其工作队列加入的分叉任务的顺序。

处于异步模式的ForkJoinPool中的工作人员以FIFO(先进先出)顺序处理此类任务。默认情况下,ForkJoinPool以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));
            }
        }
    }
}

非异步模式ForkJoinPool的默认设置)中,从未加入的分叉任务以LIFO顺序执行。

当您以非异步模式运行示例程序时,查看一个worker的输出,您可能会看到如下模式:

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

这里,任务10,11,12被分叉,然后一旦工人开始执行它们,就会以相反的顺序执行。

另一方面,在异步模式中,再次查看一个工作人员的输出,该模式将如下所示:

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

任务8,9,10,11分叉,然后按照提交的顺序执行。

何时使用哪种模式?每当选择ForkJoinPool线程池来利用其工作窃取属性而不是递归fork / join任务处理时,异步模式可能是更自然的选择,因为任务按它们提交的顺序执行。

异步事件驱动的框架(如CompletableFuture)有时被称为从异步模式中获利。例如,在构造CompletableFuture回调的复杂链时,异步模式下的自定义ForkJoinPool执行程序可能比默认执行程序稍微好一些。 (虽然我不能从经验中说出来。)

答案 1 :(得分:5)

它适用于已提交但从未加入的事件样式任务。 所以基本上是为副作用执行的任务,而不是返回加入后由分叉任务处理的结果。

相关问题