什么是最佳线程池大小的简单程序运行基于cpu的Java任务 [英] What is optimum thread pool size for simple program running cpu based tasks in Java

查看:228
本文介绍了什么是最佳线程池大小的简单程序运行基于cpu的Java任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用线程池来执行任务,大多是cpu基于一点I / O,
的大小大于cpus的数量。

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.

这是正确的,如果是真的问题或大多是理论上的,即如果我将线程池大小增加到1000,我会注意到巨大的差异。

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.

推荐答案

如果你有CPU限制的任务,你增加线程数,增加开销和性能降低。注意:拥有比等待任务更多的线程只是浪费资源,但可能不会减缓任务这么多。

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.

我会使用多个(例如1或2 )的cpus数,而不是只添加一个,因为有一个太多的线程可以有惊人的开销。

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.

这篇关于什么是最佳线程池大小的简单程序运行基于cpu的Java任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆