Java thread affinity Java thread affinity multithreading multithreading

Java thread affinity


You can't do this in pure java. But if you really need it -- you can use JNI to call native code which do the job. This is the place to start with:

http://ovatman.blogspot.com/2010/02/using-java-jni-to-set-thread-affinity.html

http://blog.toadhead.net/index.php/2011/01/22/cputhread-affinity-in-java/

UPD: After some thinking, I've decided to create my own class for this: ThreadAffinity.java It's JNA-based, and very simple -- so, if you want to use it in production, may be you should spent some time making it more stable, but for benchmarking and testing it works well as is.

UPD 2: There is another library for working with thread affinity in java. It uses same method as previously noted, but has another interface


I know it's been a while, but if anyone comes across this thread, here's how I solved this problem. I wrote a script that would do the following:

  1. "jstack -l "
  2. Take the results, find the "nid"'s of the threads I want to manually lock down to cores.
  3. Taskset those threads.