Tutorial :Java thread running on which processor?



Question:

Is there a way to find out which processor (either on a single system or mutliple systems) your thread is running on, using Java native threads? If not, is there any library which could help?


Solution:1

The JVM's thread scheduler is JVM-specific, so there is no 'universal' solution. As far as I know there is nothing available out-of-the-box, but perhaps using:

  • the Sun JVM;
  • Solaris - or Mac, as Tom Hawtin - tackline points out;
  • DTrace.

you might have some luck:

  • trace a thread-start probe, which has as args[3] the "The native/OS thread ID. This is the ID assigned by the host operating system "
  • map the native/OS thread ID to a CPU, using Solaris-specific utilities.


Solution:2

I've never heard of such a call, and I highly doubt there is one, as it's not really necessary, and would require extra platform-specific code.


Solution:3

As far as I know the standard JDK does not support it (at least up to JDK 6). If that's what you really need, you'll probably need to execute some native calls using JNI. A nice example could be found here (while it's not exactly what you need, I believe it's a good start).

There is a lot of other information you can get from the JDK, by the way, using the ThreadMXBean class (like CPU usage per thread), and maybe you can find what you're looking for there.


Solution:4

The OS will schedule threads on different processors at different times. So even if you get a snapshot of where each thread is running at any given moment, it could be out of date within milli-seconds.

What is the problem you are trying to solve? Perhaps to can do what you want without having to know this.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »