Class Hk2ThreadLocal<T>

java.lang.Object
org.glassfish.hk2.utilities.general.Hk2ThreadLocal<T>

public class Hk2ThreadLocal<T> extends 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 Details

  • Constructor Details

    • Hk2ThreadLocal

      public Hk2ThreadLocal()
  • Method Details

    • 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