• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 246
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Marshal
Posts: 4501
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 246
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Being a smart alec beats the alternative. This tiny ad knows what I'm talking about:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic