01: /*
02: * @(#)InheritableThreadLocal.java 1.19 06/10/10
03: *
04: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
05: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
06: *
07: * This program is free software; you can redistribute it and/or
08: * modify it under the terms of the GNU General Public License version
09: * 2 only, as published by the Free Software Foundation.
10: *
11: * This program is distributed in the hope that it will be useful, but
12: * WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14: * General Public License version 2 for more details (a copy is
15: * included at /legal/license.txt).
16: *
17: * You should have received a copy of the GNU General Public License
18: * version 2 along with this work; if not, write to the Free Software
19: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20: * 02110-1301 USA
21: *
22: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
23: * Clara, CA 95054 or visit www.sun.com if you need additional
24: * information or have any questions.
25: *
26: */
27:
28: package java.lang;
29:
30: import java.lang.ref.*;
31:
32: /**
33: * This class extends <tt>ThreadLocal</tt> to provide inheritance of values
34: * from parent thread to child thread: when a child thread is created, the
35: * child receives initial values for all inheritable thread-local variables
36: * for which the parent has values. Normally the child's values will be
37: * identical to the parent's; however, the child's value can be made an
38: * arbitrary function of the parent's by overriding the <tt>childValue</tt>
39: * method in this class.
40: *
41: * <p>Inheritable thread-local variables are used in preference to
42: * ordinary thread-local variables when the per-thread-attribute being
43: * maintained in the variable (e.g., User ID, Transaction ID) must be
44: * automatically transmitted to any child threads that are created.
45: *
46: * @author Josh Bloch and Doug Lea
47: * @version 1.19, 10/10/06
48: * @see ThreadLocal
49: * @since 1.2
50: */
51:
52: public class InheritableThreadLocal extends ThreadLocal {
53: /**
54: * Computes the child's initial value for this inheritable thread-local
55: * variable as a function of the parent's value at the time the child
56: * thread is created. This method is called from within the parent
57: * thread before the child is started.
58: * <p>
59: * This method merely returns its input argument, and should be overridden
60: * if a different behavior is desired.
61: *
62: * @param parentValue the parent thread's value
63: * @return the child thread's initial value
64: */
65: protected Object childValue(Object parentValue) {
66: return parentValue;
67: }
68:
69: /**
70: * Get the map associated with a ThreadLocal.
71: *
72: * @param t the current thread
73: */
74: ThreadLocalMap getMap(Thread t) {
75: return t.inheritableThreadLocals;
76: }
77:
78: /**
79: * Create the map associated with a ThreadLocal.
80: *
81: * @param t the current thread
82: * @param firstValue value for the initial entry of the table.
83: * @param map the map to store.
84: */
85: void createMap(Thread t, Object firstValue) {
86: t.inheritableThreadLocals = new ThreadLocalMap(this, firstValue);
87: }
88: }
|