Improved build.xml
[vimdoclet.git] / sample / java.lang.ThreadLocal.txt
blob387092aa18c6513469e282e980e762bad06972e2
1 *java.lang.ThreadLocal* *ThreadLocal* This class provides thread-local variables
3 public class ThreadLocal
4   extends    |java.lang.Object|
6 |java.lang.ThreadLocal_Description|
7 |java.lang.ThreadLocal_Fields|
8 |java.lang.ThreadLocal_Constructors|
9 |java.lang.ThreadLocal_Methods|
11 ================================================================================
13 *java.lang.ThreadLocal_Constructors*
14 |java.lang.ThreadLocal()|Creates a thread local variable.
16 *java.lang.ThreadLocal_Methods*
17 |java.lang.ThreadLocal.get()|Returns the value in the current thread's copy of 
18 |java.lang.ThreadLocal.initialValue()|Returns the current thread's initial valu
19 |java.lang.ThreadLocal.remove()|Removes the value for this ThreadLocal.
20 |java.lang.ThreadLocal.set(T)|Sets the current thread's copy of this thread-loc
22 *java.lang.ThreadLocal_Description*
24 This class provides thread-local variables. These variables differ from their 
25 normal counterparts in that each thread that accesses one (via its get or set 
26 method) has its own, independently initialized copy of the variable. 
27 ThreadLocal instances are typically private static fields in classes that wish 
28 to associate state with a thread (e.g., a user ID or Transaction ID). 
30 For example, in the class below, the private static ThreadLocal instance 
31 (serialNum) maintains a "serial number" for each thread that invokes the 
32 class's static SerialNum.get() method, which returns the current thread's 
33 serial number. (A thread's serial number is assigned the first time it invokes 
34 SerialNum.get(), and remains unchanged on subsequent calls.) 
36 public class SerialNum { // The next serial number to be assigned private 
37 static int nextSerialNum = 0; 
39 private static ThreadLocal serialNum = new ThreadLocal() { protected 
40 synchronized Object initialValue() { return new Integer(nextSerialNum++); } }; 
42 public static int get() { return ((Integer) (serialNum.get())).intValue(); } } 
44 Each thread holds an implicit reference to its copy of a thread-local variable 
45 as long as the thread is alive and the ThreadLocal instance is accessible; 
46 after a thread goes away, all of its copies of thread-local instances are 
47 subject to garbage collection (unless other references to these copies exist). 
50 *java.lang.ThreadLocal()*
52 public ThreadLocal()
54 Creates a thread local variable. 
57 *java.lang.ThreadLocal.get()*
59 public |java.lang.Object| get()
61 Returns the value in the current thread's copy of this thread-local variable. 
62 Creates and initializes the copy if this is the first time the thread has 
63 called this method. 
66     Returns: the current thread's value of this thread-local 
67 *java.lang.ThreadLocal.initialValue()*
69 protected |java.lang.Object| initialValue()
71 Returns the current thread's initial value for this thread-local variable. This 
72 method will be invoked at most once per accessing thread for each thread-local, 
73 the first time the thread accesses the variable with the 
74 (|java.lang.ThreadLocal|) method. The initialValue method will not be invoked 
75 in a thread if the thread invokes the (|java.lang.ThreadLocal|) method prior to 
76 the get method. 
78 This implementation simply returns null; if the programmer desires thread-local 
79 variables to be initialized to some value other than null, ThreadLocal must be 
80 subclassed, and this method overridden. Typically, an anonymous inner class 
81 will be used. Typical implementations of initialValue will invoke an 
82 appropriate constructor and return the newly constructed object. 
85     Returns: the initial value for this thread-local 
86 *java.lang.ThreadLocal.remove()*
88 public void remove()
90 Removes the value for this ThreadLocal. This may help reduce the storage 
91 requirements of ThreadLocals. If this ThreadLocal is accessed again, it will by 
92 default have its initialValue. 
95 *java.lang.ThreadLocal.set(T)*
97 public void set(java.lang.Object value)
99 Sets the current thread's copy of this thread-local variable to the specified 
100 value. Many applications will have no need for this functionality, relying 
101 solely on the (|java.lang.ThreadLocal|) method to set the values of 
102 thread-locals. 
104     value - the value to be stored in the current threads' copy of this thread-local.