7

What I'm wondering about (and what documentation I find is not very helpful in figuring it out), is what happens to a CPU core when the Thread that is executing on it transfers control to hardware device stuff (disk controller, network I/O, ...) to do some stuff that the CPU/core cannot help with. Does that core become available for executing other Threads, or does it just stall and wait (even if there are other Threads with CPU work to do that are available for scheduling) ?

The oft-given advice of "as many Threads as cores" seems to suggest the latter.

Erwin Smout
  • 18,113
  • 4
  • 33
  • 52
  • 1
    Java uses native OS threads. Therefore thread scheduling is the job of the operating system. The Java runtime just creates the threads. – Robert Apr 02 '15 at 08:02
  • 4
    The "as many threads as cores" advice is adequate when the tasks are CPU-bound. If they're doing networking IO, more threads can be used. And yes, of course, if a thread doesn't need the CPU, the OS scheduler will let another thread use it. – JB Nizet Apr 02 '15 at 08:05
  • Thx. Acceptable as an answer. – Erwin Smout Apr 02 '15 at 08:10
  • 1
    Actually, in modern systems which use *Hyper-Threading*, it could be `2 X Number of cores`. What is done to threads internally is dependant on lots of factors.. For example, processor affinity could allow certain cores to go idle for some time and certain cores to work continuously because switching the processor would be terribly slow.. So how the CPU behaves is different on different implementations.. Like JB Nizet says, that rule is used with CPU bound threads in mind. And OS will most likely schedule threads so as to keep none of the cores idle.. – TheLostMind Apr 02 '15 at 08:20

1 Answers1

1

That's out of control to Java. The scheduling is done by the OS and therefore outside of the scope for the JVM.

It's very likely that the core is reclaimed by the OS when it is waiting for some IO to be done.

The simple advice "one thread per core/processor" is for CPU intensive operations. If you know that most of the time you're waiting for IO then you can create more threads than cores are there.

Also note that enabled Hyper-Threading counts towards the number of available processors so a quad-core processor with enabled Hyper-Threading will be reported a having 8 available processors (see also this question).

Uwe Plonus
  • 9,803
  • 4
  • 41
  • 48