Java 每个处理器的线程数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/215236/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Threads per Processor
提问by Leo
In Java, is there a programmatic way to find out how many concurrent threads are supported by a CPU?
在 Java 中,是否有一种编程方式来找出 CPU 支持多少并发线程?
Update
更新
To clarify, I'm not trying to hammer the CPU with threads and I am aware of Runtime.getRuntime().availableProcessors() function, which provides me part of the information I'm looking for.
澄清一下,我不是想用线程敲打 CPU,我知道 Runtime.getRuntime().availableProcessors() 函数,它为我提供了我正在寻找的部分信息。
I want to find out if there's a way to automatically tune the size of thread pool so that:
我想知道是否有办法自动调整线程池的大小,以便:
- if I'm running on a 1-year old server, I get 2 threads (1 thread per CPU x an arbitrary multiplier of 2)
- if I switch to an Intel i7 quad core two years from now (which supports 2 threads per core), I get 16 threads (2 logical threads per CPU x 4 CPUs x the arbitrary multiplier of 2).
- if, instead, I use a eight core Ultrasparc T2 server (which supports 8 threads per core), I get 128 threads (8 threads per CPU x 8 CPUs x the arbitrary multiplier of 2)
- if I deploy the same software on a cluster of 30 different machines, potentially purchased at different years, I don't need to read the CPU specs and set configuration options for every single one of them.
- 如果我在一台使用了 1 年的服务器上运行,我会得到 2 个线程(每个 CPU 1 个线程 x 2 的任意乘数)
- 如果我从现在起两年后切换到 Intel i7 四核(每个内核支持 2 个线程),我会得到 16 个线程(每个 CPU 2 个逻辑线程 x 4 个 CPU x 2 的任意乘数)。
- 相反,如果我使用八核 Ultrasparc T2 服务器(支持每核 8 个线程),我会得到 128 个线程(每个 CPU 8 个线程 x 8 个 CPU x 2 的任意乘数)
- 如果我在 30 台不同机器的集群上部署相同的软件,可能在不同的年份购买,我不需要阅读 CPU 规格并为每台机器设置配置选项。
回答by JesperE
A CPU does not normally pose a limit on the number of threads, and I don't think Java itself has a limit on the number of native (kernel) threads it will spawn.
CPU 通常不会限制线程数,而且我认为 Java 本身对它产生的本机(内核)线程数没有限制。
There is a method availableProcessors()in the Runtime class. Is that what you're looking for?
Runtime 类中有一个availableProcessors()方法。这就是你要找的吗?
回答by pipTheGeek
A single non-hyperthreading CPU core can always run one thread. You can spawn lots of threads and the CPU will switch between them.
单个非超线程 CPU 内核始终可以运行一个线程。您可以生成大量线程,CPU 将在它们之间切换。
The best number depends on the task. If it is a task that will take lots of CPU power and not require any I/O (like calculating pi, prime numbers, etc.) then 1 thread per CPU will probably be best. If the task is more I/O bound. like processing information from disk, then you will probably get better performance by having more than one thread per CPU. In this case the disk access can take place while the CPU is processing information from a previous disk read.
最佳数量取决于任务。如果这是一项需要大量 CPU 能力且不需要任何 I/O(例如计算 pi、质数等)的任务,那么每个 CPU 1 个线程可能是最好的。如果任务受更多 I/O 限制。就像处理来自磁盘的信息一样,那么每个 CPU 有一个以上的线程可能会获得更好的性能。在这种情况下,可以在 CPU 处理来自先前磁盘读取的信息时进行磁盘访问。
I suggest you do some testing of how performance in your situation scales with number of threads per CPU core and decide based on that. Then, when your application runs, it can check availableProcessors()
and decide how many threads it should spawn.
Hyperthreading will make the single core appear to the operating system and all applications, including availableProcessors()
, as 2 CPUs, so if your application can use hyperthreading you will get the benefit. If not, then performance will suffer slightly but probably not enough to make the extra effort in catering for it worth while.
我建议你做一些测试,看看你的情况下的性能如何随每个 CPU 内核的线程数而变化,并据此做出决定。然后,当您的应用程序运行时,它可以检查availableProcessors()
并决定应该产生多少线程。超线程将使操作系统和所有应用程序(包括availableProcessors()
2 个 CPU)看到单核,因此如果您的应用程序可以使用超线程,您将获得好处。如果没有,那么性能会受到轻微影响,但可能不足以为满足它而付出额外的努力。
回答by tvanfosson
This is a function of the VM, not the CPU. It has to do with the amount of heap consumed per thread. When you run out of space on the heap, you're done. As with other posters, I suspect your app becomes unusable before this point if you exceed the heap space because of thread count.
这是 VM 的功能,而不是 CPU。它与每个线程消耗的堆量有关。当堆上的空间用完时,您就完成了。与其他海报一样,如果由于线程数而超出堆空间,我怀疑您的应用程序在此之前变得无法使用。
See this discussion.
请参阅此讨论。
回答by artur02
Basics: Application loaded into memory is a process. A process has at least 1 thread. If you want, you can create as many threads as you want in a process (theoretically). So number of threads depends upon you and the algorithms you use.
基础知识:加载到内存中的应用程序是一个进程。一个进程至少有 1 个线程。如果需要,您可以在一个进程中创建任意数量的线程(理论上)。所以线程数取决于你和你使用的算法。
If you use thread pools, that means thread pool manages the number of threads because creating a thread consumes resources. Thread pools recycle threads. This means many logical threads can run inside one physical thread one after one.
如果使用线程池,则意味着线程池管理线程数,因为创建线程会消耗资源。线程池回收线程。这意味着许多逻辑线程可以一个接一个地在一个物理线程中运行。
You don't have to consider the number of threads, it's managed by the thread pool algorithms. Thread pools choose different algorithms for servers and desktop machines (OSes).
您不必考虑线程数,它由线程池算法管理。线程池为服务器和台式机 (OS) 选择不同的算法。
Edit1:You can use explicit threadsif you think thread pool doesn't use the resources you have. You can manage the number of threads explicitly in that case.
编辑 1:如果您认为线程池不使用您拥有的资源,则 可以使用显式线程。在这种情况下,您可以明确管理线程数。
回答by Kibbee
Each processor, or processor core, can do exactly 1 thing at a time. With hyperthreading, things get a little different, but for the most part that still remains true, which is why my HT machine at work almost never goes above 50%, and even when it's at 100%, it's not processing twice as much at once.
每个处理器或处理器内核一次只能做一件事情。使用超线程,情况会有所不同,但在大多数情况下仍然如此,这就是为什么我的 HT 机器在工作时几乎从未超过 50%,即使在 100% 时,它也不会同时处理两倍.
You'll probably just have to do some testing on common architectures you plan to deploy on to determine how many threads you want to run on each CPU. Just using 1 thread may be too slow if you're waiting for a lot of I/O. Running a lot of threads will slow things down as the processor will have to switch threads more often, which can be quite costly. I'm not sure if there is any hard-coded limit to how many threads you can run, but I gaurantee that your app would probably come to a crawl from too much thread switching before you reached any kind of hard limit. Ultimately, you should just leave it as an option in the configuration file, so that you can easily tune your app to whatever processor you're running it on.
您可能只需要对计划部署的常见架构进行一些测试,以确定要在每个 CPU 上运行的线程数。如果您正在等待大量 I/O,仅使用 1 个线程可能会太慢。运行大量线程会减慢速度,因为处理器将不得不更频繁地切换线程,这可能会非常昂贵。我不确定您可以运行的线程数是否有任何硬编码限制,但我保证您的应用程序可能会在达到任何类型的硬限制之前从过多的线程切换中爬行。最终,您应该将它作为配置文件中的一个选项,以便您可以轻松地将您的应用程序调整到您运行它的任何处理器。
回答by Adam Davis
There is no standard way to get the number of supported threads per CPU core within Java. Your best bet is to get a Java CPUID utility that gives you the processor information, and then match it against a table you'll have to generate that gives you the threads per core that the processor manages without a "real" context switch.
在 Java 中没有获得每个 CPU 内核支持的线程数的标准方法。最好的办法是获得一个 Java CPUID 实用程序,它为您提供处理器信息,然后将它与您必须生成的表进行匹配,该表为您提供处理器管理的每个内核的线程,而无需“真正的”上下文切换。
-Adam
-亚当
回答by Tom Hawtin - tackline
Runtime.availableProcessors returns the number of logical processors (i.e. hardware threads) not physical cores. See CR 5048379.
Runtime.availableProcessors 返回逻辑处理器(即硬件线程)而不是物理内核的数量。参见CR 5048379。