简体   繁体   English

为什么我的多线程Java程序无法最大化我的计算机上的所有内核?

[英]Why is my multithreaded Java program not maxing out all my cores on my machine?

I have a program that starts up and creates an in-memory data model and then creates a (command-line-specified) number of threads to run several string checking algorithms against an input set and that data model. 我有一个程序,该程序可以启动并创建一个内存中的数据模型,然后创建一个(命令行指定的)线程数,以针对输入集和该数据模型运行几种字符串检查算法。 The work is divided amongst the threads along the input set of strings, and then each thread iterates the same in-memory data model instance (which is never updated again, so there are no synchronization issues). 工作沿着输入字符串集在线程之间分配,然后每个线程迭代相同的内存中数据模型实例(该实例不再进行更新,因此没有同步问题)。

I'm running this on a Windows 2003 64-bit server with 2 quadcore processors, and from looking at Windows task Manager they aren't being maxed-out, (nor are they looking like they are being particularly taxed) when I run with 10 threads. 我正在带有2个四核处理器的Windows 2003 64位服务器上运行此程序,而从Windows Task Manager来看,它们并没有被用尽(在运行它们时,它们看起来也没有受到特别的负担) 10个线程。 Is this normal behaviour? 这是正常行为吗?

It appears that 7 threads all complete a similar amount of work in a similar amount of time, so would you recommend running with 7 threads instead? 看来7个线程都在相似的时间内完成了相似的工作量,因此,您是否建议使用7个线程来运行?

Should I run it with more threads?...Although I assume this could be detrimental as the JVM will do more context switching between the threads. 我应该用更多的线程运行它吗?...尽管我认为这样做可能有害,因为JVM将在线程之间进行更多的上下文切换。

Alternatively, should I run it with fewer threads? 另外,我应该用更少的线程运行它吗?

Alternatively, what would be the best tool I could use to measure this?...Would a profiling tool help me out here - indeed, is one of the several profilers better at detecting bottlenecks (assuming I have one here) than the rest? 或者,什么是我可以用来衡量此问题的最佳工具?...配置文件工具是否可以在这里帮助我-的确,在检测瓶颈(假设我在这里有一个)的情况下,几个探查器之一比其他更好吗?

Note, the server is also running SQL Server 2005 (this may or may not be relevant), but nothing much is happening on that database when I am running my program. 请注意,服务器也正在运行SQL Server 2005(这可能相关,也可能不相关),但是当我运行程序时,该数据库上没有发生任何事情。

Note also, the threads are only doing string matching, they aren't doing any I/O or database work or anything else they may need to wait on. 还要注意,线程仅在进行字符串匹配,而没有在进行任何I / O或数据库工作,也可能在等待其他任何事情。

My guess would be that your app is bottlenecked on memory access, ie your CPU cores spend most of the time waiting for data to be read from main memory. 我的猜测是您的应用程序在内存访问方面遇到瓶颈,即您的CPU内核大部分时间都在等待从主内存中读取数据。 I'm not sure how well profilers can diagnose this kind of problem (the profiler itself could influence the behaviour considerably). 我不确定剖析器能否很好地诊断此类问题(剖析器本身可能会严重影响行为)。 You could verify the guess by having your code repeat the operations it does many times on a very small data set. 您可以通过让代码在非常小的数据集上重复执行多次操作来验证猜测。

If this guess is correct, the only thing you can do (other than getting a server with more memory bandwidth) is to try and increase the locality of your memory access to make better use of caches; 如果这个猜想是正确的,那么您唯一可以做的事情(除了使服务器具有更大的内存带宽外)是尝试增加内存访问的位置,以更好地利用缓存。 but depending on the details of the application that may not be possible. 但是根据应用程序的细节,可能无法实现。 Using more threads may in fact lead to worse performance because of cores sharing cache memory. 实际上,由于内核共享高速缓存,使用更多线程可能会导致性能下降。

Without seeing the actual code, it's hard to give proper advice. 没有看到实际的代码,很难给出适当的建议。 But do make sure that the threads aren't locking on shared resources, since that would naturally prevent them all from working as efficiently as possible. 但是请确保线程没有锁定共享资源,因为这自然会阻止它们全部尽可能高效地工作。 Also, when you say they aren't doing any io, are they not reading an input or writing an output either? 另外,当您说他们不执行任何io时,他们也不是在读取输入还是在写输出吗? this could also be a bottleneck. 这也可能是瓶颈。

With regards to cpu intensive threads, it is normally not beneficial to run more threads than you have actual cores, but in an uncontrolled environment like this with other big apps running at the same time, you are probably better off simply testing your way to the optimal number of threads. 对于CPU密集型线程,通常运行多于实际内核的线程是无益的,但是在这样的不受控制的环境中,同时运行其他大型应用程序,最好测试一下自己的方法以达到更好的效果。最佳线程数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM