java.lang
Class InheritableThreadLocal

java.lang.Object
  |
  +--java.lang.ThreadLocal
        |
        +--java.lang.InheritableThreadLocal

public class InheritableThreadLocal
extends ThreadLocal

This class extends ThreadLocal to provide inheritance of values from parent Thread to child Thread: when a child thread is created, the child receives initial values for all InheritableThreadLocals for which the parent has values. Normally the child's values will be identical to the parent's; however, the child's value can be made an arbitrary function of the parent's by overriding the childValue method in this class.

InheritableThreadLocal variables are used in preference to ordinary ThreadLocal variables when the per-thread-attribute being maintained in the variable (e.g., User ID, Transaction ID) must be automatically transmitted to any child threads that are created.

Since:
JDK1.2
See Also:
ThreadLocal

Inner Class Summary
(package private)  class InheritableThreadLocal.Entry
          The information associated with an (InheritableThreadLocal,Thread) pair.
 
Inner classes inherited from class java.lang.ThreadLocal
ThreadLocal.Entry
 
Fields inherited from class java.lang.ThreadLocal
map
 
Constructor Summary
InheritableThreadLocal()
          Creates an InheritableThreadLocal variable.
 
Method Summary
(package private) static void bequeath(Thread parent, Thread child)
          Passes the ThreadLocal values represented by the specified list of Entries onto the specified child Thread.
protected  Object childValue(Object parentValue)
          Computes the child's initial value for this InheritableThreadLocal as a function of the parent's value at the time the child Thread is created.
(package private)  ThreadLocal.Entry newEntry(Object value)
          Overrides method in ThreadLocal and implements inheritability, in conjunction with the bequeath method.
 
Methods inherited from class java.lang.ThreadLocal
get, initialValue, ourEntry, set
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, toString, wait, wait, wait
 

Constructor Detail

InheritableThreadLocal

public InheritableThreadLocal()
Creates an InheritableThreadLocal variable.
Method Detail

childValue

protected Object childValue(Object parentValue)
Computes the child's initial value for this InheritableThreadLocal as a function of the parent's value at the time the child Thread is created. This method is called from within the parent thread before the child is started.

This method merely returns its input argument, and should be overridden if a different behavior is desired.


bequeath

static void bequeath(Thread parent,
                     Thread child)
Passes the ThreadLocal values represented by the specified list of Entries onto the specified child Thread. (The child's value is computed from the parent's as per the childValue method.) This method is invoked (only) at Thread creation time, by Thread.init.

newEntry

ThreadLocal.Entry newEntry(Object value)
Overrides method in ThreadLocal and implements inheritability, in conjunction with the bequeath method.
Overrides:
newEntry in class ThreadLocal