java - ThreadPoolExecutor 与 ForkJoinPool : stealing subtasks

标签 java multithreading java.util.concurrent threadpoolexecutor forkjoinpool

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/

相关文章:

java - 如何使用notify/wait()唤醒/挂起特定(组)线程?

java - 安全使用 CyclicBarrier.reset

java - AWS 无法验证提供的访问凭证

C# TCP 聊天应用线程

java - 在 Java 中使用多个具有相同标签的 JButton

java - 结束线程的执行而不使用其 stop() 方法

java - 在四核上使用 Executors.newFixedThreadPool 进行 2 次或 4 次 Future 提交没有区别

java - 如何知道另一个线程是否进行 SynchronousQueue 轮询?

java - 如何使用 MongoDB 获取连续的行集?

java - Java 后端 API