29 import java.util.Map;
30 import java.util.WeakHashMap;
31
32 /**
33 * Abstract class that maps Class objects to lazily-computed values of
34 * type V. A concrete subclass must implement the computeValue method
35 * to determine how the values are computed.
36 *
37 * The keys are only weakly reachable through this map, so this map
38 * does not prevent a class (along with its class loader, etc.) from
39 * being garbage collected if it is not otherwise strongly reachable.
40 * The values are only softly reachable through this map, so that the
41 * computed values generally persist while not otherwise strongly
42 * reachable, but their storage may be reclaimed if necessary. Also,
43 * note that if a key is strongly reachable from a value, then the key
44 * is effectively softly reachable through this map, which may delay
45 * garbage collection of classes (see 4429536).
46 **/
47 public abstract class WeakClassHashMap<V> {
48
49 private Map<Class<?>,ValueCell<V>> internalMap =
50 new WeakHashMap<Class<?>,ValueCell<V>>();
51
52 protected WeakClassHashMap() { }
53
54 public V get(Class<?> remoteClass) {
55 /*
56 * Use a mutable cell (a one-element list) to hold the soft
57 * reference to a value, to allow the lazy value computation
58 * to be synchronized with entry-level granularity instead of
59 * by locking the whole table.
60 */
61 ValueCell<V> valueCell;
62 synchronized (internalMap) {
63 valueCell = internalMap.get(remoteClass);
64 if (valueCell == null) {
65 valueCell = new ValueCell<V>();
66 internalMap.put(remoteClass, valueCell);
67 }
68 }
69 synchronized (valueCell) {
70 V value = null;
|
29 import java.util.Map;
30 import java.util.WeakHashMap;
31
32 /**
33 * Abstract class that maps Class objects to lazily-computed values of
34 * type V. A concrete subclass must implement the computeValue method
35 * to determine how the values are computed.
36 *
37 * The keys are only weakly reachable through this map, so this map
38 * does not prevent a class (along with its class loader, etc.) from
39 * being garbage collected if it is not otherwise strongly reachable.
40 * The values are only softly reachable through this map, so that the
41 * computed values generally persist while not otherwise strongly
42 * reachable, but their storage may be reclaimed if necessary. Also,
43 * note that if a key is strongly reachable from a value, then the key
44 * is effectively softly reachable through this map, which may delay
45 * garbage collection of classes (see 4429536).
46 **/
47 public abstract class WeakClassHashMap<V> {
48
49 private Map<Class<?>,ValueCell<V>> internalMap = new WeakHashMap<>();
50
51 protected WeakClassHashMap() { }
52
53 public V get(Class<?> remoteClass) {
54 /*
55 * Use a mutable cell (a one-element list) to hold the soft
56 * reference to a value, to allow the lazy value computation
57 * to be synchronized with entry-level granularity instead of
58 * by locking the whole table.
59 */
60 ValueCell<V> valueCell;
61 synchronized (internalMap) {
62 valueCell = internalMap.get(remoteClass);
63 if (valueCell == null) {
64 valueCell = new ValueCell<V>();
65 internalMap.put(remoteClass, valueCell);
66 }
67 }
68 synchronized (valueCell) {
69 V value = null;
|