
Thread-Local Allocation Buffers (TLABs).Practical examples of optimizations considering Thread Affinity of Java objects are 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. 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. typically used by only one thread most of the time. Second, in Java there is the observation that in typical programs objects are thread-affine, i.e. But there are 3rd party libraries, as mentioned by other answers.


In case of Java, there is no standard API to get control over this. This CPU affinity for threads is often also called “thread affinity”.

This must be handled by the operating system. First, it’s preferable that threads have an affinity to a certain CPU (core) to make the most of their CPU-local caches.
