Detailed difference between Java8 ForkJoinPool and Executors.newWorkStealingPool?

Work stealing is a technique used by modern thread-pools in order to decrease contention on the work queue.

A classical threadpool has one queue, and each thread-pool-thread locks the queue, dequeue a task and then unlocks the queue. if the tasks are short and there are many of them, there is a lot of contention on the queue. using a lock-free queue really helps here, but doesn't solve the problem entirely.

Modern thread pools use work stealing - each thread has its own queue. when a threadpool thread produces a task - it enqueues it to his own queue. when a threadpool thread wants to dequeue a task - it first tries to dequeue a task out of his own queue and if it doesn't have any - it "steals" work from other thread queues. this really decreases the contention of the theradpool and improve performance.

newWorkStealingPool creates a workstealing-utilizing thread pool with the number of threads as the number of processors.

newWorkStealingPool presents a new problem. if I have four logical cores, then the pool will have four threads total. if my tasks block - for example on synchronous IO - I don't utilize my CPUs enough. what I want is four active threads at any given moment, for example - four threads which encrypt AES and another 140 threads which wait for the IO to finish.

this is what ForkJoinPool provides - if your task spawns new tasks and that task waits for them to finish - the pool will inject new active threads in order to saturate the CPU. it is worth mentioning that ForkJoinPool utilizes work stealing too.

which one to use? if you work with the fork-join model or you know your tasks block indefinitely, use the ForkJoinPool. if your tasks are short and are mostly CPU-bound, use newWorkStealingPool.

and after anything has being said, modern applications tend to use thread pool with the number of processors available and utilize asynchronous IO and lock-free-containers in order to prevent blocking. this (usually) gives the best performance.

 

来源:https://stackoverflow.com/questions/41337451/detailed-difference-between-java8-forkjoinpool-and-executors-newworkstealingpool

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值