如何在java中的并行程序中获得理想的线程数? [英] How to get ideal number of threads in parallel programs in java?

查看:126
本文介绍了如何在java中的并行程序中获得理想的线程数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在批处理程序中获得理想的线程数,该程序在支持并行模式的批处理框架中运行,如Spring Batch中的并行步骤。

I need to get an ideal number of threads in a batch program, which runs in batch framework supporting parallel mode, like parallel step in Spring Batch.

目前为止据我所知,执行程序步骤的线程太多并不好,它可能会对程序的性能产生负面影响。一些因素可能会导致性能下降(上下文切换,使用共享资源时的竞争条件(锁定,同步......)......(还有其他因素吗?))。

As far as I know, it is not good that there are too many threads to execute steps of a program, it may has negative effect to the performance of the program. Some factors could arise performance degradation(context switching, race condition when using shared resources(locking, sync..) ... (are there any other factors?)).

当然,获得理想线程数的最佳方法是让我有实际的程序测试来调整程序的线程数。但在我的情况下,实际测试并不容易,因为测试需要很多东西(人员,测试计划,测试数据等等),这对我来说现在很难准备。所以,在进行实际测试之前,我想知道如何尽可能地获得我的程序中可猜测的理想线程数。
我应该考虑什么来获得我的程序的理想线程数(步数)? CPU核心数?我的程序运行的机器上的进程数?数据库连接数?
在这样的情况下,有没有合理的方法,比如公式?

Of course the best way of getting the ideal number of threads is for me to have actual program tests adjusting the number of threads of the program. But in my situation, it is not that easy to have the actual test because many things are needed for the tests(persons, test scheduling, test data, etc..), which are too difficult for me to prepare now. So, before getting the actual tests, I want to know the way of getting a guessable ideal number of threads of my program, as best as I can. What should I consider to get the ideal number of threads(steps) of my program?? number of CPU cores?? number of processes on a machine on which my program would run?? number of database connection?? Is there a rational way such as a formula in a situation like this?

推荐答案

最重要的考虑因素是您的应用程序/计算是CPU绑定的或IO绑定的。

The most important consideration is whether your application/calculation is CPU-bound or IO-bound.


  • 如果它受IO限制(单个线程花费大部分时间等待外部资源,如数据库连接,文件系统或其他外部数据源)然后你可以分配(许多)线程而不是可用处理器的数量 - 当然多少还取决于外部资源的扩展程度 - 本地文件系统,可能不是那么多。

  • 如果它(主要)是CPU绑定的,那么稍微超过
    可用处理器的数量可能是最好的。

这篇关于如何在java中的并行程序中获得理想的线程数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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