1 |
/* ThreadLocal -- a variable with a unique value per thread |
/* ThreadLocal -- a variable with a unique value per thread |
2 |
Copyright (C) 2000, 2002 Free Software Foundation, Inc. |
Copyright (C) 2000, 2002, 2003 Free Software Foundation, Inc. |
3 |
|
|
4 |
This file is part of GNU Classpath. |
This file is part of GNU Classpath. |
5 |
|
|
37 |
|
|
38 |
package java.lang; |
package java.lang; |
39 |
|
|
40 |
|
import java.util.Collections; |
41 |
import java.util.Map; |
import java.util.Map; |
42 |
import java.util.WeakHashMap; |
import java.util.WeakHashMap; |
43 |
|
|
102 |
* <code>set(Thread, Object)</code> and <code>get(Thread)</code> methods |
* <code>set(Thread, Object)</code> and <code>get(Thread)</code> methods |
103 |
* access it. Package visible for use by InheritableThreadLocal. |
* access it. Package visible for use by InheritableThreadLocal. |
104 |
*/ |
*/ |
105 |
final Map valueMap = new WeakHashMap(); |
final Map valueMap = Collections.synchronizedMap(new WeakHashMap()); |
106 |
|
|
107 |
/** |
/** |
108 |
* Creates a ThreadLocal object without associating any value to it yet. |
* Creates a ThreadLocal object without associating any value to it yet. |
136 |
{ |
{ |
137 |
Thread currentThread = Thread.currentThread(); |
Thread currentThread = Thread.currentThread(); |
138 |
// 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 |
139 |
// ever modify the returned value. |
// ever modify the returned value and valueMap is a synchronizedMap. |
140 |
Object value = valueMap.get(currentThread); |
Object value = valueMap.get(currentThread); |
141 |
if (value == null) |
if (value == null) |
142 |
{ |
{ |
157 |
public void set(Object value) |
public void set(Object value) |
158 |
{ |
{ |
159 |
// 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 |
160 |
// ever modify the returned value. |
// ever modify the returned value and valueMap is a synchronizedMap. |
161 |
valueMap.put(Thread.currentThread(), value == null ? NULL : value); |
valueMap.put(Thread.currentThread(), value == null ? NULL : value); |
162 |
} |
} |
163 |
} |
} |