11

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?

Roman
  • 64,384
  • 92
  • 238
  • 332

5 Answers5

18

You can use

Runtime.getRuntime().availableProcessors()

But its more of a best guess and even mentioned by the 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.

John Vint
  • 39,695
  • 7
  • 78
  • 108
  • 1
    +1 - Note: this doesn't tell the application how many processors/cores are *installed*. But it does tell it how many are *available* to the JVM ... which is what the OP really needs. – Stephen C Dec 24 '10 at 00:31
  • @Stephen C: yes, I need exactly that. But, could you enlighten me a little: when is it possible that not all of the processors are available to the JVM? – Roman Dec 24 '10 at 00:35
  • @Roman: I imagine that when the OS reserves some cores/processors for some other process, they won't be available to the JVM. – FrustratedWithFormsDesigner Dec 24 '10 at 01:02
  • @Roman if I am using a computer, a random app has approximately zero right to use all my cores just because it feels like it. If I am trying to do other stuff at the same time, both my JVM and my OS might limit how many cores your Java app gets to use - either automatically by detecting that I am trying to doing other stuff, or because I've configured things to do that. (Notably, giving you access to all cores might give you opportunity for side-channel timing attacks, and might be on average slower to keep context-switching all cores, due to per-core L1 caches and so on.) – mtraceur Jun 08 '22 at 21:46
7

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.

jtahlborn
  • 52,909
  • 5
  • 76
  • 118
4

Runtime.getRuntime().availableProcessors();

Cajunluke
  • 3,103
  • 28
  • 28
2

How about (code snippets speak a 1000 words):

 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(); 
 }
}
Stephen Gennard
  • 1,910
  • 16
  • 21
1

Yes on windows for sure. JNA with kernel32.dll. Use SYSTEM_INFO structure from GetNativeSystemInfo call. There is dwNumberOfProcessors among other things.

I believe this will provide the actual number of processors installed, not the number available.

brian_d
  • 11,190
  • 5
  • 47
  • 72
  • That's a rather difficult (and non-portable) way of doing this. – user541686 Dec 24 '10 at 00:02
  • Agreed, though it does give the number of processors. I just remember that field from previously using the SYSTEM_INFO structure for a different purpose. – brian_d Dec 24 '10 at 00:03