Class Hk2ThreadLocal<T>


  • public class Hk2ThreadLocal<T>
    extends java.lang.Object
    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

      Fields 
      Modifier and Type Field Description
      private java.util.WeakHashMap<java.lang.Thread,​T> locals  
      private java.util.concurrent.locks.ReentrantReadWriteLock readWriteLock  
      private java.util.concurrent.locks.ReentrantReadWriteLock.ReadLock rLock  
      private java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock wLock  
    • Constructor Summary

      Constructors 
      Constructor Description
      Hk2ThreadLocal()  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      T get()
      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 initialValue()
      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 removeAll()
      Removes all threads current thread's value for this thread-local variable.
      void set​(T value)
      Sets the current thread's copy of this thread-local variable to the specified value.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • 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
    • Constructor Detail

      • Hk2ThreadLocal

        public Hk2ThreadLocal()
    • 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 the get() method, unless the thread previously invoked the set(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 of remove() followed by get().

        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 the initialValue() 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 the initialValue() 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 its initialValue() 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 its initialValue() 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