Java thread running on which processor?

匆匆过客 提交于 2019-12-10 13:06:22

问题


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?


回答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.



回答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.




回答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.




回答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.



来源:https://stackoverflow.com/questions/974277/java-thread-running-on-which-processor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!