Java Threadpool大小和availableProcessors() [英] Java Threadpool size and availableProcessors()

查看:83
本文介绍了Java Threadpool大小和availableProcessors()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个并行运行(全天)任务的程序(要执行的任务中没有I / O)所以我使用了 Executors.newFixedThreadPool(poolSize)实现它。

I have a program which runs (all day) tasks in parallel (no I/O in the task to be executed) so I have used Executors.newFixedThreadPool(poolSize) to implement it.

最初我将 poolSize 设置为 Runtime.getRuntime() .availableProcessors(),但我有点担心使用所有可用内核,因为在同一台PC上运行其他进程(32个内核)。

Initially I set the poolSize to Runtime.getRuntime().availableProcessors(), but I was a bit worried to use all the available cores since there are other processes running on the same PC (32 cores).

特别是我有十个其他JVM运行相同的程序(在不同的输入数据上),所以我有点担心在可用内核之间的线程切换方面可能会有很多开销,这可能会减慢整体计算。

In particular I have ten other JVM running the same program (on different input data), so I'm a bit worried that there might be a lot of overhead in terms of threads switching amongst the available cores, which could slow down the overall calculations.

我如何确定每个程序/ JVM的池大小?

How shall I decide the size of the pool for each program / JVM?

此外,在我的PC中,还有其他进程一直在运行(防病毒,备份等)。我也应该考虑这些吗?

Also, in my PC, there are other processes running all the time (Antivirus, Backup, etc.). Shall I take into account these as well?

推荐答案


我该如何决定游泳池的大小对于每个程序/ JVM?

How shall I decide the size of the pool for each program / JVM?

您需要的线程数将使您获得接近99%的利用率,而不是更多。

You want the number of threads which will get you close to 99% utilisation and no more.

平衡工作的最简单方法是让进程运行一次,同时处理多个文件并只使用一个线程池。如果需要通过命令行启动文件,可以将进程设置为服务。

The simplest way to balance the work is to have the process running once, processing multiple files at concurrently and using just one thread pool. You can set up you process as a service if you need to start files via the command line.

如果出于某种原因这是不可能的,则需要估计多少线程池应该缩小。尝试运行一个进程并查看利用率。如果一个人说40%,那么我怀疑十个过程被400%过度使用。也就是说,你可以将池大小减少4倍。

If this is impossible for some reason, you will need to guesstimate how much the thread pools should be shrunk by. Try running one process and look at the utilisation. If one is say 40% then I suspect ten processes is over utilised by 400%. i.e then you might reduce the pool size by a factor of 4.

这篇关于Java Threadpool大小和availableProcessors()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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