I want to make some tuning for multithreaded program.
我想为多线程程序做一些调优。
If I know how much threads can really work in parallel I can make the program much more effective.
如果我知道有多少线程可以并行工作,我可以使程序更有效。
Is there a way in Java to get this info?
在Java中有什么方法可以获得这个信息吗?
5 个解决方案
#1
17
You can use
您可以使用
Runtime.getRuntime().availableProcessors()
But its more of a best guess and even mentioned by the API
但它更像是一个最好的猜测,甚至被API所提及
This value may change during a particular invocation of the virtual machine. Applications that are sensitive to the number of available processors should therefore occasionally poll this property and adjust their resource usage appropriately.
在虚拟机的特定调用期间,此值可能会改变。因此,对可用处理器数量敏感的应用程序应该偶尔轮询此属性并适当调整它们的资源使用。
#2
6
One note about the availableProcessors() method, it does not distinguish between physical cpus and virtual cpus. e.g., if you have hyperthreading enabled on your computer the number will be double the number of physical cpus (which is a little frustrating). unfortunately, there is no way to determine real vs. virtual cpus in pure java.
关于availableprocessor()方法的一点注意是,它没有区分物理cpu和虚拟cpu。例如,如果您的计算机上启用了超线程,那么这个数字将是物理cpu数量的两倍(这有点令人沮丧)。不幸的是,没有办法在纯java中确定真正的cpu和虚拟cpu。
#3
4
Runtime.getRuntime().availableProcessors();
.availableProcessors Runtime.getRuntime()();
#4
2
How about (code snippets speak a 1000 words):
如何(代码片段讲1000个单词):
public class Main {
/**
* Displays the number of processors available in the Java Virtual Machine
*/
public void displayAvailableProcessors() {
Runtime runtime = Runtime.getRuntime();
int nrOfProcessors = runtime.availableProcessors();
System.out.println("Number of processors available to the Java Virtual Machine: " + nrOfProcessors);
}
public static void main(String[] args) {
new Main().displayAvailableProcessors();
}
}
#5
1
Yes on windows for sure. JNA with kernel32.dll. Use SYSTEM_INFO
structure from GetNativeSystemInfo
call. There is dwNumberOfProcessors
among other things.
在windows上是肯定的。JNA kernel32.dll。使用GetNativeSystemInfo调用的SYSTEM_INFO结构。还有dwnumberofprocessor(处理器)。
I believe this will provide the actual number of processors installed, not the number available.
我相信这将提供实际安装的处理器数量,而不是可用的数量。
#1
17
You can use
您可以使用
Runtime.getRuntime().availableProcessors()
But its more of a best guess and even mentioned by the API
但它更像是一个最好的猜测,甚至被API所提及
This value may change during a particular invocation of the virtual machine. Applications that are sensitive to the number of available processors should therefore occasionally poll this property and adjust their resource usage appropriately.
在虚拟机的特定调用期间,此值可能会改变。因此,对可用处理器数量敏感的应用程序应该偶尔轮询此属性并适当调整它们的资源使用。
#2
6
One note about the availableProcessors() method, it does not distinguish between physical cpus and virtual cpus. e.g., if you have hyperthreading enabled on your computer the number will be double the number of physical cpus (which is a little frustrating). unfortunately, there is no way to determine real vs. virtual cpus in pure java.
关于availableprocessor()方法的一点注意是,它没有区分物理cpu和虚拟cpu。例如,如果您的计算机上启用了超线程,那么这个数字将是物理cpu数量的两倍(这有点令人沮丧)。不幸的是,没有办法在纯java中确定真正的cpu和虚拟cpu。
#3
4
Runtime.getRuntime().availableProcessors();
.availableProcessors Runtime.getRuntime()();
#4
2
How about (code snippets speak a 1000 words):
如何(代码片段讲1000个单词):
public class Main {
/**
* Displays the number of processors available in the Java Virtual Machine
*/
public void displayAvailableProcessors() {
Runtime runtime = Runtime.getRuntime();
int nrOfProcessors = runtime.availableProcessors();
System.out.println("Number of processors available to the Java Virtual Machine: " + nrOfProcessors);
}
public static void main(String[] args) {
new Main().displayAvailableProcessors();
}
}
#5
1
Yes on windows for sure. JNA with kernel32.dll. Use SYSTEM_INFO
structure from GetNativeSystemInfo
call. There is dwNumberOfProcessors
among other things.
在windows上是肯定的。JNA kernel32.dll。使用GetNativeSystemInfo调用的SYSTEM_INFO结构。还有dwnumberofprocessor(处理器)。
I believe this will provide the actual number of processors installed, not the number available.
我相信这将提供实际安装的处理器数量,而不是可用的数量。