- java.lang.Object
-
- io.netty5.util.concurrent.FastThreadLocal<V>
-
- Type Parameters:
V
- the type of the thread-local variable
public class FastThreadLocal<V> extends Object
A special variant ofThreadLocal
that yields higher access performance when accessed from aFastThreadLocalThread
.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 byDefaultThreadFactory
areFastThreadLocalThread
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 regularThreadLocal
.- See Also:
ThreadLocal
-
-
Constructor Summary
Constructors Constructor Description FastThreadLocal()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static void
destroy()
Destroys the data structure that keeps allFastThreadLocal
variables accessed from non-FastThreadLocalThread
s.V
get()
Returns the current value for the current threadV
getIfExists()
Returns the current value for the current thread if it exists,null
otherwise.protected V
initialValue()
Returns the initial value for this thread-local variable.boolean
isSet()
Returnstrue
if and only if this thread-local variable is set.protected void
onRemoval(V value)
Invoked when this thread local variable is removed byremove()
.void
remove()
Sets the value to uninitialized for the specified thread local map.static void
removeAll()
Removes allFastThreadLocal
variables bound to the current thread.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.
-
-
-
Method Detail
-
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.
-
get
public final V get()
Returns the current value for the current thread
-
getIfExists
public final V getIfExists()
Returns the current value for the current thread if it exists,null
otherwise.
-
set
public final void set(V value)
Set the value for the current thread.
-
isSet
public final boolean isSet()
Returnstrue
if and only if this thread-local variable is set.
-
remove
public final void 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().
-
initialValue
protected V initialValue() throws Exception
Returns the initial value for this thread-local variable.- Throws:
Exception
-
onRemoval
protected void onRemoval(V value) throws Exception
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
-
-