100%CPU使用率的Java线程优化 [英] Java threading optimization at 100% CPU usage

查看:671
本文介绍了100%CPU使用率的Java线程优化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序接受队列上的工作,然后旋转工作,以便在独立线程上完成。线程数量不大,比如高达100,但这些都是密集型任务,可以快速将CPU提升到100%。

I have an application that accepts work on a queue and then spins that work off to be completed on independent threads. The number of threads is not massive, say up to 100, but these are intensive tasks and can quickly bump the CPU up to 100%.

完成大部分工作最快的:当我需要做更多工作并让Java线程调度程序处理分配工作时,我最好只启动更多线程,或者更聪明地管理工作负载以保持CPU低于100%让我更快?

To get the most work done the quickest: am I best off to just launch more threads when I need to do more work and let the Java thread scheduler handle distributing the work, or would getting smarter and managing the work load to keep the CPU below 100% get me further faster?

该机器专用于我的java应用程序。

The machine is dedicated to my java app.

编辑:

感谢您的精彩输入!

这些任务的复杂性各不相同,涉及I / O,因此线程较低说4的池可能只是将CPU运行到20%。我无法知道有多少任务实际上将CPU带到100%。

The tasks are of varying complexity and involve I/O so having a low thread pool of say 4 may well be only running the CPU to 20%. I have no way of knowing how many tasks will actually take the CPU to 100%.

我的想法是我应该通过RMI监控CPU并动态拨打上下工作,或者我应该不关心并让操作系统处理它。

My thought was should I monitor CPU through RMI and dynamically dial the work up and down, or should I just not care and let the OS handle it.

推荐答案

如果你有太多的同时计算密集型任务平行线程,你很快就会达到收益递减的程度。实际上,如果有N个处理器(核心),那么你不需要超过N个这样的线程。现在,如果任务偶尔会暂停I / O或用户交互,那么正确的数字可能会稍大一些。但总的来说,如果在任何一个时刻有更多的线程想要进行计算而不是有可用的核心,那么你的程序就会浪费时间在上下文切换上 - 也就是说,调度会让你付出代价。

If you have too many simultaneous compute-intensive tasks in parallel threads, you reach the point of diminishing returns very quickly. In fact, if there are N processors (cores), then you don't want more than N such threads. Now, if the tasks occasionally pause for I/O or user interaction, then the right number can be somewhat larger. But in general, if at any one moment there are more threads that want to do computation than there are cores available, then your program is wasting time on context switches -- i.e., the scheduling is costing you.

这篇关于100%CPU使用率的Java线程优化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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