Package java.lang
Class ThreadLocal<T>
- java.lang.Object
-
- java.lang.ThreadLocal<T>
-
- Direct Known Subclasses:
InheritableThreadLocal
public class ThreadLocal<T> extends Object
Implements a thread-local storage, that is, a variable for which each thread has its own value. All threads share the sameThreadLocalobject, but each sees a different value when accessing it, and changes made by one thread do not affect the other threads. The implementation supportsnullvalues.- See Also:
Thread
-
-
Constructor Summary
Constructors Constructor Description ThreadLocal()Creates a new thread-local variable.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Tget()Returns the value of this variable for the current thread.protected TinitialValue()Provides the initial value of this variable for the current thread.voidremove()Removes the entry for this variable in the current thread.voidset(T value)Sets the value of this variable for the current thread.
-
-
-
Method Detail
-
get
public T get()
Returns the value of this variable for the current thread. If an entry doesn't yet exist for this variable on this thread, this method will create an entry, populating the value with the result ofinitialValue().- Returns:
- the current value of the variable for the calling thread.
-
initialValue
protected T initialValue()
Provides the initial value of this variable for the current thread. The default implementation returnsnull.- Returns:
- the initial value of the variable.
-
set
public void set(T value)
Sets the value of this variable for the current thread. If set tonull, the value will be set to null and the underlying entry will still be present.- Parameters:
value- the new value of the variable for the caller thread.
-
remove
public void remove()
Removes the entry for this variable in the current thread. If this call is followed by aget()before aset(T),#get()will callinitialValue()and create a new entry with the resulting value.- Since:
- 1.5
-
-