Class AdvancedThreadLocal<T>

java.lang.Object
rs.baselib.util.AdvancedThreadLocal<T>

public class AdvancedThreadLocal<T>
extends java.lang.Object
Extends functionality of ThreadLocal by allowing to access values from other threads.

The need comes from threads that leave behind resources, however cannot detect the problem themselfs.

Since:
1.2.9
Author:
ralph
  • Constructor Summary

    Constructors 
    Constructor Description
    AdvancedThreadLocal()
    Constructor.
  • Method Summary

    Modifier and Type Method Description
    void clear()
    Clears all values on all threads.
    T get()
    Returns the value in the current thread's copy of this thread-local variable.
    java.util.Collection<java.util.Map.Entry<java.lang.Thread,​T>> getEntries()
    Returns the current values of this set.
    protected T initialValue()
    Returns the current thread's "initial value" for this thread-local variable.
    T remove()
    Removes the current thread's value for this thread-local variable.
    T set​(T value)
    Sets the current thread's copy of this thread-local variable to the specified value.
    void verifyThreads()
    Removes all values where the thried died meanwhile.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

  • 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 initial value for this thread-local
    • set

      public T 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 ThreadLocal.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.
      Returns:
      the old value
      See Also:
      ThreadLocal.set(java.lang.Object)
    • remove

      public T 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.
      Returns:
      value removed
      See Also:
      ThreadLocal.remove()
    • getEntries

      public java.util.Collection<java.util.Map.Entry<java.lang.Thread,​T>> getEntries()
      Returns the current values of this set.
      Returns:
      the current values
    • verifyThreads

      public void verifyThreads()
      Removes all values where the thried died meanwhile.
    • clear

      public void clear()
      Clears all values on all threads.