01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17:
18: package java.lang;
19:
20: /**
21: * A ThreadLocal is a variable that has a per-thread value. Different Threads
22: * may reference the same ThreadLocal object, but the values they observe will
23: * not be <code>==</code>. This provides Thread local storage.
24: *
25: * @see java.lang.Thread
26: */
27: public class ThreadLocal<T> {
28: /**
29: * Constructs a new ThreadLocal object
30: */
31: public ThreadLocal() {
32: super ();
33: }
34:
35: /**
36: * Return the value of this variable under
37: * <code>Thread.currentThread()</code>
38: */
39: @SuppressWarnings("unchecked")
40: public T get() {
41: return (T) Thread.currentThread().getThreadLocal(this );
42: }
43:
44: /**
45: * Return the initial value of this variable under
46: * <code>Thread.currentThread()</code>
47: */
48: protected T initialValue() {
49: return null;
50: }
51:
52: /**
53: * Set the value of this variable under <code>Thread.currentThread()</code>
54: */
55: public void set(T value) {
56: Thread.currentThread().setThreadLocal(this , value);
57: }
58:
59: /**
60: * <p>
61: * Removes the current value, such that the next call to {@link #get()} will
62: * return the default value, as defined by {@link #initialValue()}.
63: * </p>
64: *
65: * @since 1.5
66: */
67: public void remove() {
68: /*
69: * TODO Consider adding an explicit removeThreadLocal method to Thread
70: * for VM implementations to take advantage of extra possible space.
71: */
72: Thread.currentThread().setThreadLocal(this, initialValue());
73: }
74: }
|