Class Hk2ThreadLocal<T>
- java.lang.Object
-
- org.glassfish.hk2.utilities.general.Hk2ThreadLocal<T>
-
public class Hk2ThreadLocal<T> extends java.lang.ObjectThis is a poor mans version of aThreadLocalwith the one major upside of aremoveAll()method that can be used to remove ALL instances of all thread locals on ALL threads from any other thread.
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.WeakHashMap<java.lang.Thread,T>localsprivate java.util.concurrent.locks.ReentrantReadWriteLockreadWriteLockprivate java.util.concurrent.locks.ReentrantReadWriteLock.ReadLockrLockprivate java.util.concurrent.locks.ReentrantReadWriteLock.WriteLockwLock
-
Constructor Summary
Constructors Constructor Description Hk2ThreadLocal()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Tget()Returns the value in the current thread's copy of this thread-local variable.intgetSize()Returns the total size of the internal data structure in terms of entries.protected TinitialValue()Returns the current thread's "initial value" for this thread-local variable.voidremove()Removes the current thread's value for this thread-local variable.voidremoveAll()Removes all threads current thread's value for this thread-local variable.voidset(T value)Sets the current thread's copy of this thread-local variable to the specified value.
-
-
-
Field Detail
-
readWriteLock
private final java.util.concurrent.locks.ReentrantReadWriteLock readWriteLock
-
wLock
private final java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock wLock
-
rLock
private final java.util.concurrent.locks.ReentrantReadWriteLock.ReadLock rLock
-
locals
private final java.util.WeakHashMap<java.lang.Thread,T> locals
-
-
Method Detail
-
initialValue
protected T 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
public T 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
public void set(T value)
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
-
-