6

I have this question about the JVM running my Java code. My friend has a 2.4 or 2.5GHz dual core while I have a 2GHz quad core. Now my question is: does Java use all the cores or just one? My friend thought Java used 1 core and so he would have the better running time because his core has a higher clock rate than mine.

Sumit Singh
  • 15,743
  • 6
  • 59
  • 89
user1903753
  • 77
  • 1
  • 4
  • 2
    Java will use as many cores as it needs to run all the concurrent `Thread`s a JVM application runs. – Zutty Dec 14 '12 at 10:48
  • If you don't have a GUI and don't use Multithreading in your application, your program will only use one core. – jlordo Dec 14 '12 at 10:49

1 Answers1

6

The JVM itself uses several threads, which will most likely use several cores (depending on the mood of the OS scheduler).

Your program uses as many threads as you ask it to use. If your code is single-threaded, it will be sequential and will not benefit from your multi-core architecture (note however, that your single thread might use more than one core, but not at the same time).

assylias
  • 321,522
  • 82
  • 660
  • 783
  • I never specifically ask for more threads, it's just a program that will run sequentially, so it only uses 1 core? – user1903753 Dec 14 '12 at 11:03
  • @user1903753 In that case your code will only use one core. But as mentioned above and by others, the JVM will still benefit from having more cores to run background tasks such as garbage collection. – assylias Dec 14 '12 at 11:05