gpt4 book ai didi

java - Java 7 中的类值

转载 作者:IT老高 更新时间:2023-10-28 20:52:10 25 4
gpt4 key购买 nike

在浏览 Java 7 API 文档时,我偶然发现了新类 java.lang.ClassValue使用以下相当少的文档:

Lazily associate a computed value with (potentially) every type. For example, if a dynamic language needs to construct a message dispatch table for each class encountered at a message send call site, it can use a ClassValue to cache information needed to perform the message send quickly, for each class encountered.

谁能更好地解释这个类解决了什么问题,或者一些示例代码或已经使用这个类的开源项目?

更新:我仍然对使用这个新类的一些实际源代码或示例感兴趣。

我还找到了 this mail on the mlvm-dev mailing list关于一些实现改进。它显然已从使用 Wea​​kHashMap 更改为 java.lang.Class 上的新私有(private)字段,以使其更具可扩展性。

最佳答案

这个类的目的最好的解释是它解决了Java Bug 6389107

在许多用例中,人们希望本质上拥有 Map<Class<?>, T>。出于某种原因,但这会引起各种麻烦,因为 Class在 Map 之前,对象将无法进行 GC。 WeakHashMap<Class<?>, T>不能解决问题,因为非常频繁,T引用类。

上面的错误进行了更详细的解释,并包含面临此问题的示例项目/代码。

ClassValue 是这个问题的答案。一种线程安全的类加载器加载/卸载安全方式,用于将数据与类关联。

关于java - Java 7 中的类值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7444420/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com