Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In JVM, are Thread objects tied directly to CPU cores, or is there a Mapper in between?

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.

like image 406
Erwin Smout Avatar asked Apr 02 '15 07:04

Erwin Smout


People also ask

Do Java threads use multiple cores?

Java will benefit from multiple cores, if the OS distribute threads over the available processors. JVM itself do not do anything special to get its threads scheduled evenly across multiple cores.

Are threads executed on different cores?

The answer is: It depends. On a system with multiple processors or CPU cores (as is common with modern processors), multiple processes or threads can be executed in parallel. On a single processor, though, it is not possible to have processes or threads truly executing at the same time.

How do Java threads work?

A thread is created by instantiating a Thread object, or an object that extends Thread , but the thread doesn't start to execute until the start() method is called on the new Thread object. Threads end when they come to the end of their run() method or throw an unhandled exception.

Does Java use hyperthreading?

Hyperthreading is enabled by Java as it uses native threads, thus this is a OS/CPU config. However Hyperthreading does not give you extra cores, it permits timeshare of the four cpus that you have.


1 Answers

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).

like image 57
Uwe Plonus Avatar answered Sep 16 '22 14:09

Uwe Plonus