javamultithreadingconcurrencyterminology

What does thread affinity mean?


Somewhere I have heard about thread affinity and library Thread Affinity Executor. But I cannot find a proper reference for it at least in Java. What is it all about?


Solution

  • There are two issues. First, it’s preferable that threads have an affinity to a certain CPU (core) to make the most of their CPU-local caches. This must be handled by the operating system. This CPU affinity for threads is often also called “thread affinity”. In case of Java, there isn't any standard API to get control over this. But there are third-party libraries, as mentioned by other answers.

    Second, in Java there is the observation that in typical programs objects are thread-affine, i.e., typically used by only one thread most of the time. So it’s the task of the JVM’s optimizer to ensure that objects affine to one thread are placed close to each other in memory to fit into one CPU’s cache, but place objects affine to different threads not too close to each other to avoid that they share a cache line, as otherwise two CPUs/Cores have to synchronize them too often.

    The ideal situation is that a CPU can work on some objects independently to another CPU working on other objects placed in an unrelated memory region.

    Practical examples of optimizations considering Thread Affinity of Java objects are