Java 7中的ClassValue [英] ClassValue in Java 7

查看:319
本文介绍了Java 7中的ClassValue的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

While browsing the Java 7 API documentation I stumbled upon the new class java.lang.ClassValue with the following rather minimal documentation:


懒惰地将计算值与(可能)每种类型相关联。例如,如果动态语言需要为消息发送调用站点遇到的每个类构建消息调度表,它可以使用 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.

任何人都可以更好地解释这个类解决的问题,也许还有一些示例代码或已经使用过这个类的开源项目?

Can anyone give a better explanation of what problem this class solves and perhaps some sample code or open source project that already uses this class?

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

Update: I'm still interested in some actual source code or examples using this new class.

我还找到了关于某些实施改进的mlvm-dev邮件列表上的这封邮件。它显然已经从使用WeakHashMap变为java.lang.Class上的新私有字段,以使其更具可伸缩性。

I also found this mail on the mlvm-dev mailing list concerning some implementation improvements. It was apparently changed from using a WeakHashMap to a new private field on java.lang.Class to make it more scalable.

推荐答案

对本课程目的的最佳解释是它解决了 Java Bug 6389107

The best explanation of the purpose of this class is that it solves Java Bug 6389107

有许多用例希望基本上有一个 Map< Class<?> ;, T> for某种原因,但这会导致各种麻烦,因为 Class 对象在Map之前不会是GC。 WeakHashMap< Class<?> ;, T> 无法解决问题,因为非常频繁, T 引用该类。

There are many use cases where one wants to essentially have a Map<Class<?>, T> for some reason, but this causes all sorts of trouble since Class objects will then not be GC-able until the Map is. WeakHashMap<Class<?>, T> doesn't solve the problem because very frequently, T references the class.

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

The bug above goes into a much more detailed explanation and contains example projects/code that face this problem.

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

ClassValue is the answer to this problem. A thread-safe, classloader loading/unloading safe way to associate data with a Class.

这篇关于Java 7中的ClassValue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆