Why Use Thread Affinitycross

If you have an application that is running with multiple threads, most of the time the thread scheduler works well.

The problem is that sometimes, in rare cases the thread scheduler may suspend your important thread and give time to a less important thread. Every time a thread is stopped, it has to go to sleep and when it wakes up the cache lines have to be reloaded. Waking up takes time and if the work the thread does is short lived with pauses in between tasks, it can mean these tasks always run slow, they can run between, 2 to 5 times slower.
There maybe some threads which you never want to go to sleep,  you don’t want them stopping to let another thread run for a bit.  You want them running all the time on their own CPUs, possibly their own Cores.
Allocating a thread manually is tedious, so the thread affinity library makes it easier by being declarative. You say; thread X must be assigned to one Core and Y and Z should use the same Core, but different CPUs.  The library will find isolated CPUs and assign the threads based on the rules you provide, on a best effort basis, if you don’t have isolated cores it will just do its best.


  • Binds cores to threads from java, in a declarative way.
  • The same configuration will work on a variety of machines.


The Thread Affinity library can increase your applications performance by allowing threads to be bound to specific cores. This is a benefit if the data is already on the cores L1 cache because it won’t have to be reloaded. The OpenHFT Thread Affinity libary is the ideal choice if you are writing low latency code.

For More Information

GitHubSource Code
WikiTechnical Information
Source Code DocumentationJavaDoc
Stack OverflowQuestion