• Post Reply Bookmark Topic Watch Topic
  • New Topic

Is There a direct Way to Find out how many Cores in a Computer?  RSS feed

 
Kevin Simonson
Ranch Hand
Posts: 198
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there some direct way in Java to tell how many CPUs a computer has available to run threads in parallel? I took a look at class System, but none of its methods looked like they returned that value. I wrote a couple of Java classes:

and

and I was thinking I could tell from the graph drawn how many CPUs my computer had. For example, if it had four then the time for four threads to all complete would be roughly a fourth of the time for just one thread to complete the same job. Alternately, if it just had two CPUs then I would expect the time for two threads to both complete would be roughly half the time for just one thread to complete the same job, and the time for any number of threads higher than two to complete would be longer, or at least the same. But when I ran "java ThGr 8 100002 5 1300 650 1 30" I got results between those two expected results; for three threads and for four threads the job got done quicker than it did for two threads, but not by anywhere near what I expected. So how can I tell how many CPUs my computer has?
 
Ron McLeod
Saloon Keeper
Posts: 1432
207
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at SIGAR. I believe it is let you discover the number of CPU cores on the platform.
 
Henry Wong
author
Sheriff
Posts: 22861
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

If the main purpose is to figure out how many cores are available for the JVM to run code, then one way is to use the Runtime.availableProcessors() method.

Henry
 
Kevin Simonson
Ranch Hand
Posts: 198
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:If the main purpose is to figure out how many cores are available for the JVM to run code, then one way is to use the Runtime.availableProcessors() method.

Henry

Thanks, Henry; that did the trick! Turns out my laptop just has two cores.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!