ClassValue in Java 7

后端 未结 4 1830
南笙
南笙 2020-12-29 01:37

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

Lazily

4条回答
  •  太阳男子
    2020-12-29 02:18

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

    There are many use cases where one wants to essentially have a Map, 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, 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 is the answer to this problem. A thread-safe, classloader loading/unloading safe way to associate data with a Class.

提交回复
热议问题