Package io.netty.util.concurrent
Class FastThreadLocal<V>
java.lang.Object
io.netty.util.concurrent.FastThreadLocal<V>
- Type Parameters:
V
- the type of the thread-local variable
- Direct Known Subclasses:
PooledByteBufAllocator.PoolThreadLocalCache
A special variant of
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
.
- See Also:
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate static void
addToVariablesToRemove
(InternalThreadLocalMap threadLocalMap, FastThreadLocal<?> variable) static void
destroy()
Destroys the data structure that keeps allFastThreadLocal
variables accessed from non-FastThreadLocalThread
s.final V
get()
Returns the current value for the current threadfinal V
get
(InternalThreadLocalMap threadLocalMap) Returns the current value for the specified thread local map.getAndSet
(InternalThreadLocalMap threadLocalMap, V value) Set the value for the specified thread local map.Set the value for the current thread and returns the old value.final V
Returns the current value for the current thread if it exists,null
otherwise.private V
initialize
(InternalThreadLocalMap threadLocalMap) protected V
Returns the initial value for this thread-local variable.final boolean
isSet()
Returnstrue
if and only if this thread-local variable is set.final boolean
isSet
(InternalThreadLocalMap threadLocalMap) Returnstrue
if and only if this thread-local variable is set.protected void
Invoked when this thread local variable is removed byremove()
.final void
remove()
Sets the value to uninitialized for the specified thread local map and returns the old value.final void
remove
(InternalThreadLocalMap threadLocalMap) Sets the value to uninitialized for the specified thread local map.static void
Removes allFastThreadLocal
variables bound to the current thread.private V
removeAndGet
(InternalThreadLocalMap threadLocalMap) Sets the value to uninitialized for the specified thread local map.private static void
removeFromVariablesToRemove
(InternalThreadLocalMap threadLocalMap, FastThreadLocal<?> variable) final void
set
(InternalThreadLocalMap threadLocalMap, V value) Set the value for the specified thread local map.final void
Set the value for the current thread.private V
setKnownNotUnset
(InternalThreadLocalMap threadLocalMap, V value) static int
size()
Returns the number of thread local variables bound to the current thread.
-
Field Details
-
index
private final int index
-
-
Constructor Details
-
FastThreadLocal
public FastThreadLocal()
-
-
Method Details
-
removeAll
public static void removeAll()Removes allFastThreadLocal
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. -
size
public static int size()Returns the number of thread local variables bound to the current thread. -
destroy
public static void destroy()Destroys the data structure that keeps allFastThreadLocal
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. -
addToVariablesToRemove
private static void addToVariablesToRemove(InternalThreadLocalMap threadLocalMap, FastThreadLocal<?> variable) -
removeFromVariablesToRemove
private static void removeFromVariablesToRemove(InternalThreadLocalMap threadLocalMap, FastThreadLocal<?> variable) -
get
Returns the current value for the current thread -
getIfExists
Returns the current value for the current thread if it exists,null
otherwise. -
get
Returns the current value for the specified thread local map. The specified thread local map must be for the current thread. -
initialize
-
set
Set the value for the current thread. -
set
Set the value for the specified thread local map. The specified thread local map must be for the current thread. -
getAndSet
Set the value for the current thread and returns the old value. -
getAndSet
Set the value for the specified thread local map. The specified thread local map must be for the current thread. -
setKnownNotUnset
-
isSet
public final boolean isSet()Returnstrue
if and only if this thread-local variable is set. -
isSet
Returnstrue
if and only if this thread-local variable is set. The specified thread local map must be for the current thread. -
remove
public final void remove()Sets the value to uninitialized for the specified thread local map and returns the old value. After this, any subsequent call to get() will trigger a new call to initialValue(). -
remove
Sets the value to uninitialized for the specified thread local map. After this, any subsequent call to get() will trigger a new call to initialValue(). The specified thread local map must be for the current thread. -
removeAndGet
Sets the value to uninitialized for the specified thread local map. After this, any subsequent call to get() will trigger a new call to initialValue(). The specified thread local map must be for the current thread. -
initialValue
Returns the initial value for this thread-local variable.- Throws:
Exception
-
onRemoval
Invoked when this thread local variable is removed byremove()
. Be aware thatremove()
is not guaranteed to be called when the `Thread` completes which means you can not depend on this for cleanup of the resources in the case of `Thread` completion.- Throws:
Exception
-