What is optimum thread pool size for simple program running cpu based tasks in Java

做~自己de王妃 提交于 2019-11-26 18:19:19

问题


Im using a thread pool to execute tasks , that are mostly cpu based with a bit of I/O, of size one larger than the number of cpus.

Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() + 1)

Assuming case of simple program that submits all its tasks to this executor and does little else I assume having a thread pool any larger would slow things because the OS would have to timeslice it cpus more often chance to give each thread in the threadpool a chance to run.

Is that correct, and if so is this a real problem or mostly theoretical, i.e if I increased threadpool size to 1000 would I notice a massive difference.


回答1:


If you have CPU bound tasks, as you increase the number of threads you get increasing overhead and slower performances. Note: having more threads than waiting tasks is just a waste of resources, but may not slow down the tasks so much.

I would use a multiple (e.g. 1 or 2) of the number of cpus rather than adding just one as having one too many threads can have a surprising amount of overhead.




回答2:


For reference, check this description.

http://codeidol.com/java/java-concurrency/Applying-Thread-Pools/Sizing-Thread-Pools/

In short, what you have (No. CPU + 1) is optimal on average.



来源:https://stackoverflow.com/questions/12951112/what-is-optimum-thread-pool-size-for-simple-program-running-cpu-based-tasks-in-j

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!