gpt4 book ai didi

java - ThreadPoolExecutor 与 ForkJoinPool : stealing subtasks

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:50:22 25 4
gpt4 key购买 nike

From java docs,

A ForkJoinPool differs from other kinds of ExecutorService mainly by virtue of employing work-stealing: all threads in the pool attempt to find and execute subtasks created by other active tasks (eventually blocking waiting for work if none exist).

This enables efficient processing when most tasks spawn other subtasks (as do most ForkJoinTasks). When setting asyncMode to true in constructors, ForkJoinPools may also be appropriate for use with event-style tasks that are never joined.

After going through below ForkJoinPool example , Unlike ThreadPoolExecutor, I have not seen parameter to set Queue size. I did not get clue on how ForkJoinPool stealing mechanism.

//creating the ThreadPoolExecutor

ThreadPoolExecutor executorPool = new ThreadPoolExecutor(2, 10, 60, TimeUnit.SECONDS,
new ArrayBlockingQueue<Runnable>(3000), threadFactory, rejectionHandler);

Assume that I have created ThreadPoolExecutor with 10 threads and 3000 Callable tasks have been submitted. How these threads share the load of execution of sub tasks?

And How ForkJoin pool behaves differently for same use case?

最佳答案

如果您预先有 3000 个任务,并且它们不会产生其他任务,则两者的行为不会有本质上的不同:如果有 10 个线程,将同时运行 10 个任务,直到它们全部完成。

ForkJoinPool 专为您有一个或几个任务开始的情况而设计,但任务知道如何将自己拆分为子任务。在这种情况下,ForkJoinPool 被优化以允许任务检查处理线程的可用性并适本地拆分它们自己。

关于java - ThreadPoolExecutor 与 ForkJoinPool : stealing subtasks,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33448465/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com