首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >什么是ForkJoinPool异步模式

什么是ForkJoinPool异步模式
EN

Stack Overflow用户
提问于 2011-04-13 02:42:12
回答 2查看 6.3K关注 0票数 20

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

EN

回答 2

Stack Overflow用户

发布于 2016-01-10 07:38:59

ForkJoinPool中的每个工作线程都有自己的工作队列。 关注的是每个工作者处理从未从其工作队列加入的分叉任务的顺序。

处于异步模式的ForkJoinPool中的工作进程以先进先出(first in,first out)顺序处理此类任务。默认情况下,ForkJoinPool以后进先出(后进先出)的顺序处理此类任务。

需要强调的是,async模式设置只涉及从未加入的分支任务。当使用ForkJoinPool来实现最初的设计目的,即递归fork/join任务分解时,asyncMode根本不起作用。只有当worker不参与实际的fork/join处理时,它才会执行异步任务,并且只有在那时才会实际查询asyncMode标志。

下面是一个小程序,它演示了两种不同异步模式设置之间的差异:

代码语言:javascript
复制
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的默认模式)中,从未联接的分支任务将按后进先出的顺序执行。

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

代码语言:javascript
复制
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被派生,并在worker开始执行它们之后以相反的顺序执行。

另一方面,在异步模式中,再次查看一个worker的输出,该模式可能如下所示:

代码语言:javascript
复制
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执行器可能会比默认执行器执行得稍好一些。(但我不能凭经验说话。)

票数 17
EN

Stack Overflow用户

发布于 2012-02-01 01:57:47

它适用于已提交但从未联接的事件样式任务。所以基本上,执行这些任务是因为它们的副作用,而不是为了返回一个结果,该结果将在连接后由forking任务处理。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5640046

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档