1

I read somewhere sometime that we should have one JVM per core. I also read that Java is not good with multiple cores or CPUs hence scala is preferred.

Recently on a link I read that multiple threads do execute in different core if available.

Questions:

  1. does JVM takes multiple cores/CPUs in consideration and execute each thread in separate core if available?
  2. usage of scala for better usage of cores is somewhat different that just execution in separate core?
  3. if JVM executes each thread in separate core (if available) what is the meaning of one JVM per core, with a single JVM I can utilize all the cores.
  4. if answer to 1 is yes, what is the use of deploy a war/ear in multiple instances of server running in same machine.
Vinay Taneja
  • 89
  • 2
  • 8
  • 1
    I don't quite understand.. JVM *forwards* all calls to native *OS*. How does multiple cores / Single core *directly* matter to it? – TheLostMind Jul 09 '14 at 14:30
  • @TheLostMind - it does matter. In Python the GIL prevents multiple cores from being used simultaneously, even though it can execute on any single core at a given time, so this question is relevant. – Martin Konecny Jul 09 '14 at 14:34
  • Related: http://stackoverflow.com/questions/4436422/how-does-java-makes-use-of-multiple-cores – Kevin Workman Jul 09 '14 at 14:38
  • 1
    Can you source (and date) the affirmation that "one JVM per core"? I have been executing multiple threads on multiple cores without any special intervention since JDK 1.5... (multi-core machines were not so widespread before that) – tucuxi Jul 09 '14 at 14:50

1 Answers1

4
  1. Yes. Actually, all JVMs, nowadays, use native threads. So the thread scheduling and distribution across cores is done by the OS, not by the JVM.

  2. Scala executes on the JVM, just as Java. So there is no difference in how threads use cores between Java and Scala

  3. There is no reason to have one JVM per core. You could want that to avoid using two much memory per JVM, or to be able to go for version 1 to version 2 without having any service interruption, but that is a different matter.

  4. No reason linked to multithreading problems. As I said, you might want to have two JVMs to be able to shutdown one without having any service interruption, or to isolate two versions of the same app for different customers for example. But that has nothing to do with multithreading.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • You mean to say that the JVM can *directly* decide how to execute *threads*? – TheLostMind Jul 09 '14 at 14:38
  • No, quite the contrary. The thread scheduling is done by the OS. – JB Nizet Jul 09 '14 at 14:39
  • Then- How does multiple cores / Single core directly matter to it? – TheLostMind Jul 09 '14 at 14:41
  • 1
    I'm not sure what you mean by that. But it matters because some race conditions and visibility issues can only happen if multiple cores are used. So the Java Memory Model must take multiple cores into account (and it does). – JB Nizet Jul 09 '14 at 14:46