What is Thread Affinity?cross

Thread Affinity allows you to manage threads in a straightforward manner to overcome the latency issues sometimes apparent when using the thread scheduler..

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.  You want them to be running all the time on their own CPUs, and possibly their own cores.
Allocating a thread manually is tedious, so the Thread Affinity library makes it easier by being declarative. You specify that, 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.

Featurescog

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

Summary

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 open source Thread Affinity library is the ideal choice if you are writing low latency code.

For More Information

MavenDownload
GitHubSource Code
WikiTechnical Information
Source Code DocumentationJavaDoc
Stack OverflowQuestion