Class FastThreadLocal<V>
java.lang.Object
io.netty.util.concurrent.FastThreadLocal<V>
- Type Parameters:
V- the type of the thread-local variable
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:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voiddestroy()Destroys the data structure that keeps allFastThreadLocalvariables accessed from non-FastThreadLocalThreads.final Vget()Returns the current value for the current threadfinal Vget(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 VReturns the current value for the current thread if it exists,nullotherwise.protected VReturns the initial value for this thread-local variable.final booleanisSet()Returnstrueif and only if this thread-local variable is set.final booleanisSet(InternalThreadLocalMap threadLocalMap) Returnstrueif and only if this thread-local variable is set.protected voidInvoked when this thread local variable is removed byremove().final voidremove()Sets the value to uninitialized for the specified thread local map and returns the old value.final voidremove(InternalThreadLocalMap threadLocalMap) Sets the value to uninitialized for the specified thread local map.static voidRemoves allFastThreadLocalvariables bound to the current thread.final voidset(InternalThreadLocalMap threadLocalMap, V value) Set the value for the specified thread local map.final voidSet the value for the current thread.static intsize()Returns the number of thread local variables bound to the current thread.
-
Constructor Details
-
FastThreadLocal
public FastThreadLocal()
-
-
Method Details
-
removeAll
public static void removeAll()Removes allFastThreadLocalvariables 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 allFastThreadLocalvariables accessed from non-FastThreadLocalThreads. 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. -
get
Returns the current value for the current thread -
getIfExists
Returns the current value for the current thread if it exists,nullotherwise. -
get
Returns the current value for the specified thread local map. The specified thread local map must be for the current thread. -
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
-
getAndSet
Set the value for the specified thread local map. The specified thread local map must be for the current thread. -
isSet
public final boolean isSet()Returnstrueif and only if this thread-local variable is set. -
isSet
Returnstrueif 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. -
initialValue
-
onRemoval
-