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
public class FastThreadLocal<V> extends java.lang.ObjectA special variant ofThreadLocalthat yields higher access performance when accessed from aFastThreadLocalThread.Internally, a
FastThreadLocaluses 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
FastThreadLocalThreador its subtype. By default, all threads created byDefaultThreadFactoryareFastThreadLocalThreaddue 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
-
-
Field Summary
Fields Modifier and Type Field Description private intindexprivate static intvariablesToRemoveIndex
-
Constructor Summary
Constructors Constructor Description FastThreadLocal()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private static voidaddToVariablesToRemove(InternalThreadLocalMap threadLocalMap, FastThreadLocal<?> variable)static voiddestroy()Destroys the data structure that keeps allFastThreadLocalvariables accessed from non-FastThreadLocalThreads.Vget()Returns the current value for the current threadVget(InternalThreadLocalMap threadLocalMap)Returns the current value for the specified thread local map.VgetIfExists()Returns the current value for the current thread if it exists,nullotherwise.private Vinitialize(InternalThreadLocalMap threadLocalMap)protected VinitialValue()Returns the initial value for this thread-local variable.booleanisSet()Returnstrueif and only if this thread-local variable is set.booleanisSet(InternalThreadLocalMap threadLocalMap)Returnstrueif and only if this thread-local variable is set.protected voidonRemoval(V value)Invoked when this thread local variable is removed byremove().voidremove()Sets the value to uninitialized; a proceeding call to get() will trigger a call to initialValue().voidremove(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 voidremoveAll()Removes allFastThreadLocalvariables bound to the current thread.private static voidremoveFromVariablesToRemove(InternalThreadLocalMap threadLocalMap, FastThreadLocal<?> variable)voidset(InternalThreadLocalMap threadLocalMap, V value)Set the value for the specified thread local map.voidset(V value)Set the value for the current thread.private voidsetKnownNotUnset(InternalThreadLocalMap threadLocalMap, V value)static intsize()Returns the number of thread local variables bound to the current thread.
-
-
-
Method Detail
-
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.
-
addToVariablesToRemove
private static void addToVariablesToRemove(InternalThreadLocalMap threadLocalMap, FastThreadLocal<?> variable)
-
removeFromVariablesToRemove
private static void removeFromVariablesToRemove(InternalThreadLocalMap threadLocalMap, FastThreadLocal<?> variable)
-
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,nullotherwise.
-
get
public final V get(InternalThreadLocalMap threadLocalMap)
Returns the current value for the specified thread local map. The specified thread local map must be for the current thread.
-
initialize
private V initialize(InternalThreadLocalMap threadLocalMap)
-
set
public final void set(V value)
Set the value for the current thread.
-
set
public final void set(InternalThreadLocalMap threadLocalMap, V value)
Set the value for the specified thread local map. The specified thread local map must be for the current thread.
-
setKnownNotUnset
private void setKnownNotUnset(InternalThreadLocalMap threadLocalMap, V value)
-
isSet
public final boolean isSet()
Returnstrueif and only if this thread-local variable is set.
-
isSet
public final boolean isSet(InternalThreadLocalMap threadLocalMap)
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; a proceeding call to get() will trigger a call to initialValue().
-
remove
public final 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(). The specified thread local map must be for the current thread.
-
initialValue
protected V initialValue() throws java.lang.Exception
Returns the initial value for this thread-local variable.- Throws:
java.lang.Exception
-
onRemoval
protected void onRemoval(V value) throws java.lang.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:
java.lang.Exception
-
-