我想,ThreadLocal 变量是在线程本地分配缓冲区或 TLAB 中分配的,对吗?
我没有成功找到任何文档来说明是什么让某些类存储在 TLAB 中。如果您知道一些,请发布链接。
最佳答案
I was not successfull to find any document stating what exactly makes some class stored in TLAB. If you know some, please post a link.
实际上,解释就在您链接到的博客文章中:
A Thread Local Allocation Buffer (TLAB) is a region of Eden that is used for allocation by a single thread. It enables a thread to do object allocation using thread local top and limit pointers, which is faster than doing an atomic operation on a top pointer that is shared across threads.
每个线程都从自己的 Eden block (堆的“第 0 代”部分)分配内存。几乎所有内容都会在 TLAB 中存储一段时间 - 很可能你的 ThreadLocal
也是如此 - 但在 gen0 垃圾收集后它们会离开那里。 TLAB 的作用是加快分配速度,而不是让其他线程无法访问内存。来自您链接到的同一博客的更易于访问的描述是 A little thread privacy, please .
关于java - ThreadLocal是在TLAB中分配的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4919524/