Class Hk2ThreadLocal<T>
java.lang.Object
org.glassfish.hk2.utilities.general.Hk2ThreadLocal<T>
This is a poor mans version of a
ThreadLocal
with
the one major upside of a removeAll()
method that
can be used to remove ALL instances of all thread locals on
ALL threads from any other thread.-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final WeakHashMap
<Thread, T> private final ReentrantReadWriteLock
private final ReentrantReadWriteLock.ReadLock
private final ReentrantReadWriteLock.WriteLock
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionget()
Returns the value in the current thread's copy of this thread-local variable.int
getSize()
Returns the total size of the internal data structure in terms of entries.protected T
Returns the current thread's "initial value" for this thread-local variable.void
remove()
Removes the current thread's value for this thread-local variable.void
Removes all threads current thread's value for this thread-local variable.void
Sets the current thread's copy of this thread-local variable to the specified value.
-
Field Details
-
readWriteLock
-
wLock
-
rLock
-
locals
-
-
Constructor Details
-
Hk2ThreadLocal
public Hk2ThreadLocal()
-
-
Method Details
-
initialValue
Returns the current thread's "initial value" for this thread-local variable. This method will be invoked the first time a thread accesses the variable with theget()
method, unless the thread previously invoked theset(T)
method, in which case the initialValue method will not be invoked for the thread. Normally, this method is invoked at most once per thread, but it may be invoked again in case of subsequent invocations ofremove()
followed byget()
.This implementation simply returns null; if the programmer desires thread-local variables to have an initial value other than null, ThreadLocal must be subclassed, and this method overridden. Typically, an anonymous inner class will be used.
- Returns:
- the initial value for this thread-local
-
get
Returns the value in the current thread's copy of this thread-local variable. If the variable has no value for the current thread, it is first initialized to the value returned by an invocation of theinitialValue()
method.- Returns:
- the current thread's value of this thread-local
-
set
Sets the current thread's copy of this thread-local variable to the specified value. Most subclasses will have no need to override this method, relying solely on theinitialValue()
method to set the values of thread-locals.- Parameters:
value
- the value to be stored in the current thread's copy of this thread-local.
-
remove
public void remove()Removes the current thread's value for this thread-local variable. If this thread-local variable is subsequently read by the current thread, its value will be reinitialized by invoking itsinitialValue()
method, unless its value is set by the current thread in the interim. This may result in multiple invocations of the initialValue method in the current thread. -
removeAll
public void removeAll()Removes all threads current thread's value for this thread-local variable. If this thread-local variable is subsequently read by the current thread, its value will be reinitialized by invoking itsinitialValue()
method, unless its value is set by the current thread in the interim. This may result in multiple invocations of the initialValue method in the current thread. -
getSize
public int getSize()Returns the total size of the internal data structure in terms of entries. This is used for diagnostics purposes only- Returns:
- The current number of entries across all threads. This is basically the number of threads that currently have data with the Hk2ThreadLocal
-