Team LiB
Previous Section Next Section

InheritableThreadLocal<T>java.lang

Java 1.2

This class holds a thread-local value that is inherited by child threads. See ThreadLocal for a discussion of thread-local values. Note that the inheritance referred to in the name of this class is not from superclass to subclass; it is inheritance from parent thread to child thread. Like its superclass, this class has been made generic in Java 5.0. The type variable T represents the type of the referenced object.

This class is best understood by example. Suppose that an application has defined an InheritableThreadLocal object and that a certain thread (the parent thread) has a thread-local value stored in that object. Whenever that thread creates a new thread (a child thread), the InheritableThreadLocal object is automatically updated so that the new child thread has the same value associated with it as the parent thread. Note that the value associated with the child thread is independent from the value associated with the parent thread. If the child thread subsequently alters its value by calling the set( ) method of the InheritableThreadLocal, the value associated with the parent thread does not change.

By default, a child thread inherits a parent's values unmodified. By overriding the childValue( ) method, however, you can create a subclass of InheritableThreadLocal in which the child thread inherits some arbitrary function of the parent thread's value.

Figure 10-32. java.lang.InheritableThreadLocal<T>


public class InheritableThreadLocal<T> extends ThreadLocal<T> {
// Public Constructors
     public InheritableThreadLocal( );  
// Protected Instance Methods
     protected T childValue(T parentValue);  
}

    Team LiB
    Previous Section Next Section