V
- the type of the thread-local variablepublic class FastThreadLocal<V>
extends java.lang.Object
ThreadLocal
that yields higher access performance when accessed from a
FastThreadLocalThread
.
Internally, a FastThreadLocal
uses a constant index in an array, instead of using hash code and hash table,
to look for a variable. Although seemingly very subtle, it yields slight performance advantage over using a hash
table, and it is useful when accessed frequently.
To take advantage of this thread-local variable, your thread must be a FastThreadLocalThread
or its subtype.
By default, all threads created by DefaultThreadFactory
are FastThreadLocalThread
due to this reason.
Note that the fast path is only possible on threads that extend FastThreadLocalThread
, because it requires
a special field to store the necessary state. An access by any other kind of thread falls back to a regular
ThreadLocal
.
ThreadLocal
构造器和说明 |
---|
FastThreadLocal() |
限定符和类型 | 方法和说明 |
---|---|
static void |
destroy()
Destroys the data structure that keeps all
FastThreadLocal variables accessed from
non-FastThreadLocalThread s. |
V |
get()
Returns the current value for the current thread
|
V |
get(InternalThreadLocalMap threadLocalMap)
Returns the current value for the specified thread local map.
|
protected V |
initialValue()
Returns the initial value for this thread-local variable.
|
boolean |
isSet()
Returns
true if and only if this thread-local variable is set. |
boolean |
isSet(InternalThreadLocalMap threadLocalMap)
Returns
true if and only if this thread-local variable is set. |
protected void |
onRemoval(V value)
Invoked when this thread local variable is removed by
remove() . |
void |
remove()
Sets the value to uninitialized; a proceeding call to get() will trigger a call to initialValue().
|
void |
remove(InternalThreadLocalMap threadLocalMap)
Sets the value to uninitialized for the specified thread local map;
a proceeding call to get() will trigger a call to initialValue().
|
static void |
removeAll()
Removes all
FastThreadLocal variables bound to the current thread. |
void |
set(InternalThreadLocalMap threadLocalMap,
V value)
Set the value for the specified thread local map.
|
void |
set(V value)
Set the value for the current thread.
|
static int |
size()
Returns the number of thread local variables bound to the current thread.
|
public static void removeAll()
FastThreadLocal
variables bound to the current thread. This operation is useful when you
are in a container environment, and you don't want to leave the thread local variables in the threads you do not
manage.public static int size()
public static void destroy()
FastThreadLocal
variables accessed from
non-FastThreadLocalThread
s. This operation is useful when you are in a container environment, and you
do not want to leave the thread local variables in the threads you do not manage. Call this method when your
application is being unloaded from the container.public final V get()
public final V get(InternalThreadLocalMap threadLocalMap)
public final void set(V value)
public final void set(InternalThreadLocalMap threadLocalMap, V value)
public final boolean isSet()
true
if and only if this thread-local variable is set.public final boolean isSet(InternalThreadLocalMap threadLocalMap)
true
if and only if this thread-local variable is set.
The specified thread local map must be for the current thread.public final void remove()
public final void remove(InternalThreadLocalMap threadLocalMap)
protected V initialValue() throws java.lang.Exception
java.lang.Exception