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

查看:45
本文介绍了如何在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天全站免登陆