72 |
* equals. |
* equals. |
73 |
*/ |
*/ |
74 |
private static final |
private static final |
75 |
Map<Thread, ArrayList<InheritableThreadLocal<T>>> threadMap |
Map<Thread, ArrayList<InheritableThreadLocal>> threadMap |
76 |
= Collections.synchronizedMap(new WeakHashMap<Thread, ArrayList<InheritableThreadLocal<T>>>()); |
= Collections.synchronizedMap(new WeakHashMap<Thread, ArrayList<InheritableThreadLocal>>()); |
77 |
|
|
78 |
/** |
/** |
79 |
* Creates a new InheritableThreadLocal that has no values associated |
* Creates a new InheritableThreadLocal that has no values associated |
84 |
Thread currentThread = Thread.currentThread(); |
Thread currentThread = Thread.currentThread(); |
85 |
// Note that we don't have to synchronize, as only this thread will |
// Note that we don't have to synchronize, as only this thread will |
86 |
// ever modify the returned heritage and threadMap is a synchronizedMap. |
// ever modify the returned heritage and threadMap is a synchronizedMap. |
87 |
List<InheritableThreadLocal<T>> heritage = threadMap.get(currentThread); |
ArrayList<InheritableThreadLocal> heritage = threadMap.get(currentThread); |
88 |
if (heritage == null) |
if (heritage == null) |
89 |
{ |
{ |
90 |
heritage = new ArrayList<InheritableThreadLocal<T>>(); |
heritage = new ArrayList<InheritableThreadLocal>(); |
91 |
threadMap.put(currentThread, heritage); |
threadMap.put(currentThread, heritage); |
92 |
} |
} |
93 |
heritage.add(this); |
heritage.add(this); |
110 |
/** |
/** |
111 |
* Generates the childValues of all <code>InheritableThreadLocal</code>s |
* Generates the childValues of all <code>InheritableThreadLocal</code>s |
112 |
* that are in the heritage of the current Thread for the newly created |
* that are in the heritage of the current Thread for the newly created |
113 |
* childThread. Should be called from the contructor Thread. |
* childThread. Should be called from the constructor Thread. |
114 |
* |
* |
115 |
* @param childThread the newly created thread, to inherit from this thread |
* @param childThread the newly created thread, to inherit from this thread |
116 |
* @see Thread#Thread(ThreadGroup, Runnable, String) |
* @see Thread#Thread(ThreadGroup, Runnable, String) |
121 |
Thread parentThread = Thread.currentThread(); |
Thread parentThread = Thread.currentThread(); |
122 |
// Note that we don't have to synchronize, as only this thread will |
// Note that we don't have to synchronize, as only this thread will |
123 |
// ever modify the returned heritage and threadMap is a synchronizedMap. |
// ever modify the returned heritage and threadMap is a synchronizedMap. |
124 |
ArrayList<InheritableThreadLocal<T>> heritage |
ArrayList<InheritableThreadLocal> heritage |
125 |
= threadMap.get(parentThread); |
= threadMap.get(parentThread); |
126 |
if (heritage != null) |
if (heritage != null) |
127 |
{ |
{ |
128 |
threadMap.put(childThread, |
threadMap.put(childThread, |
129 |
(ArrayList<InheritableThreadLocal<T>>) heritage.clone()); |
(ArrayList<InheritableThreadLocal>) heritage.clone()); |
130 |
// Perform the inheritance. |
// Perform the inheritance. |
131 |
Iterator<InheritableThreadLocal<T>> it = heritage.iterator(); |
Iterator<InheritableThreadLocal> it = heritage.iterator(); |
132 |
int i = heritage.size(); |
int i = heritage.size(); |
133 |
while (--i >= 0) |
while (--i >= 0) |
134 |
{ |
{ |
135 |
InheritableThreadLocal<T> local = it.next(); |
InheritableThreadLocal local = it.next(); |
136 |
T parentValue = local.valueMap.get(parentThread); |
Object parentValue = local.valueMap.get(parentThread); |
137 |
if (parentValue != null) |
if (parentValue != null) |
138 |
{ |
{ |
139 |
T childValue = local.childValue(parentValue == (T) NULL |
Object childValue = local.childValue(parentValue == NULL |
140 |
? null : parentValue); |
? null : parentValue); |
141 |
local.valueMap.put(childThread, (childValue == null |
local.valueMap.put(childThread, (childValue == null |
142 |
? (T) NULL : parentValue)); |
? NULL : parentValue)); |
143 |
} |
} |
144 |
} |
} |
145 |
} |
} |