ThreadLocal是在TLAB中分配的吗?

发布于 2024-10-16 07:49:19 字数 107 浏览 5 评论 0原文

我想,ThreadLocal 变量是在线程本地分配缓冲区或 TLAB 中分配的,对吗?

我没有成功找到任何文档来说明究竟是什么使某些类存储在 TLAB 中。如果您知道一些,请发布链接。

I suppose, that ThreadLocal variables are allocated in Thread Local allocation Buffer(s) or TLABs, am I right ?

I was not successful in finding any document stating what exactly makes some class stored in TLAB. If you know some, please post a link.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

故人爱我别走 2024-10-23 07:49:19

我没有成功找到任何文档来说明究竟是什么使某些类存储在 TLAB 中。如果您知道一些,请发布链接。

实际上,解释就在您链接到的博客文章中:

线程本地分配缓冲区 (TLAB) 是 Eden 区域,用于由单个线程进行分配。它使线程能够使用线程本地顶部和限制指针进行对象分配,这比在跨线程共享的顶部指针上执行原子操作更快。

每个线程都从自己的 Eden 块(堆的“Generation 0”部分)分配内存。几乎所有内容都会在 TLAB 中存储一段时间 - 很可能您的 ThreadLocal 也是如此 - 但在第 0 代垃圾回收后,它们会从那里移走。 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.

Actually, the explanation is right there in the blog post you lnked to:

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.

Every thread allocates memory from its own chunk of Eden, the "Generation 0" part of the heap. Pretty much everything is stored in the TLAB for a period of time - quite possibly your ThreadLocals, too - but they get moved away from there after a gen0 garbage collection. TLABs are there to make allocations faster, not to make the memory unaccessible from other threads. A more accessible description from the same blog you linked to is A little thread privacy, please.

孤君无依 2024-10-23 07:49:19

不,这是怎么回事:
从 1.4 开始,Java 中的每个线程都有一个名为 threadLocals 的字段,用于保存映射。每个threadLocal都有一个结构索引,因此它不使用hashCode()。想象一个数组,每个 ThreadLocal 都保留一个槽索引。

当线程终止并且不再有对其的引用时,ThreadLocal 就会被 GC 回收。非常简单的想法。

您可以通过扩展 Thread 并添加一个字段来保存引用来实现您自己的 ThraLocal。然后将线程转换为您自己的类并获取数据。

所以它不是 TLAB,它仍然像任何其他对象一样是堆。


历史上有一些带有静态 WeakHashMap 的实现,它们访问数据的速度非常慢。

No. Here how it is:
As of 1.4 each thread in Java has a field called threadLocals where the map is kept. Each threadLocal has an index to the structure, so it doesn't use hashCode(). Imagine an array and each ThreadLocal keep a slot index.

When the thread dies and there are no more references to it, the ThreadLocals are GC'd. Very simple idea.

You can implement your own ThreaLocal(s) by extending Thread and adding a field to hold the reference. Then cast the Thread to youw own class and take the data.

So it's not TLAB, it's still the heap like any other object.


Historically there were implementations w/ static WeakHashMap which were very slow to access the data.

请爱~陌生人 2024-10-23 07:49:19

据我了解,TLAB 用于所有中小型对象的对象分配。您的 ThreadLocal 不会有任何不同的分配。

It is my understanding that TLAB is used for object allocation of all small to medium objects. Your ThreadLocal won't be allocated any differently.

无名指的心愿 2024-10-23 07:49:19

我非常确定这取决于 JVM 实现者的判断。如果愿意,他们可以将数据放入 TLAB 中,或者放入由线程 ID 键入的全局表中。 Java 语言规范往往对此类问题保持沉默,以便 JVM 作者可以在尽可能多的不同平台上部署 Java。

I'm pretty sure that this is up to the discretion of the JVM implementer. They could put the data in TLABs if they wanted to, or in a global table keyed by the thread ID. The Java Language Specification tends to be mute about these sorts of issues so that JVM authors can deploy Java on as many and as diverse platforms as possible.

流绪微梦 2024-10-23 07:49:19

我认为只有指向它的指针是,而数据本身驻留在其他一些内存区域中。请参阅 http://blogs.oracle.com/jonthecollector/entry/the_real_thinghttp://wikis.sun.com/display/MaxineVM/Threads#Threads-Threadlocalvariables

i think only the pointer to it is, while the data itself resides in some other memory area. see http://blogs.oracle.com/jonthecollector/entry/the_real_thing and http://wikis.sun.com/display/MaxineVM/Threads#Threads-Threadlocalvariables

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文