public abstract class ClassValue<T> extends Object
ClassValue to cache information needed to
 perform the message send quickly, for each class encountered.- Since:
- 1.7
- 
Constructor SummaryConstructors Modifier Constructor Description protectedClassValue()Sole constructor.
- 
Method Summary
- 
Constructor Details- 
ClassValueprotected ClassValue()Sole constructor. (For invocation by subclass constructors, typically implicit.)
 
- 
- 
Method Details- 
computeValueComputes the given class's derived value for thisClassValue.This method will be invoked within the first thread that accesses the value with the getmethod.Normally, this method is invoked at most once per class, but it may be invoked again if there has been a call to remove.If this method throws an exception, the corresponding call to getwill terminate abnormally with that exception, and no class value will be recorded.- Parameters:
- type- the type whose class value must be computed
- Returns:
- the newly computed value associated with this ClassValue, for the given class or interface
- See Also:
- get(java.lang.Class<?>),- remove(java.lang.Class<?>)
 
- 
getReturns the value for the given class. If no value has yet been computed, it is obtained by an invocation of thecomputeValuemethod.The actual installation of the value on the class is performed atomically. At that point, if several racing threads have computed values, one is chosen, and returned to all the racing threads. The typeparameter is typically a class, but it may be any type, such as an interface, a primitive type (likeint.class), orvoid.class.In the absence of removecalls, a class value has a simple state diagram: uninitialized and initialized. Whenremovecalls are made, the rules for value observation are more complex. See the documentation forremovefor more information.- Parameters:
- type- the type whose class value must be computed or retrieved
- Returns:
- the current value associated with this ClassValue, for the given class or interface
- Throws:
- NullPointerException- if the argument is null
- See Also:
- remove(java.lang.Class<?>),- computeValue(java.lang.Class<?>)
 
- 
removeRemoves the associated value for the given class. If this value is subsequently read for the same class, its value will be reinitialized by invoking itscomputeValuemethod. This may result in an additional invocation of thecomputeValuemethod for the given class.In order to explain the interaction between getandremovecalls, we must model the state transitions of a class value to take into account the alternation between uninitialized and initialized states. To do this, number these states sequentially from zero, and note that uninitialized (or removed) states are numbered with even numbers, while initialized (or re-initialized) states have odd numbers.When a thread Tremoves a class value in state2N, nothing happens, since the class value is already uninitialized. Otherwise, the state is advanced atomically to2N+1.When a thread Tqueries a class value in state2N, the thread first attempts to initialize the class value to state2N+1by invokingcomputeValueand installing the resulting value.When Tattempts to install the newly computed value, if the state is still at2N, the class value will be initialized with the computed value, advancing it to state2N+1.Otherwise, whether the new state is even or odd, Twill discard the newly computed value and retry thegetoperation.Discarding and retrying is an important proviso, since otherwise Tcould potentially install a disastrously stale value. For example:- Tcalls- CV.get(C)and sees state- 2N
- Tquickly computes a time-dependent value- V0and gets ready to install it
- Tis hit by an unlucky paging or scheduling event, and goes to sleep for a long time
- ...meanwhile, T2also callsCV.get(C)and sees state2N
- T2quickly computes a similar time-dependent value- V1and installs it on- CV.get(C)
- T2(or a third thread) then calls- CV.remove(C), undoing- T2's work
-  the previous actions of T2are repeated several times
-  also, the relevant computed values change over time: V1,V2, ...
- ...meanwhile, Twakes up and attempts to installV0; this must fail
 CV.computeValueuses locks to properly observe the time-dependent states as it computesV1, etc. This does not remove the threat of a stale value, since there is a window of time between the return ofcomputeValueinTand the installation of the new value. No user synchronization is possible during this time.- Parameters:
- type- the type whose class value must be removed
- Throws:
- NullPointerException- if the argument is null
 
 
-