Top Description Inners Fields Constructors Methods
net.sf.ehcache.util.lang

public Class VicariousThreadLocal<T>

extends ThreadLocal<T>
Class Inheritance
Imports
java.lang.ref.ReferenceQueue, .WeakReference, java.util.concurrent.atomic.AtomicReferenceFieldUpdater

A drop-in replacement ThreadLocal implementation that does not leak when thread-local values reference the ThreadLocal object. The code is optimised to cope with frequently changing values.

In comparison to plain ThreadLocal, this implementation:

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
VicariousThreadLocal.Holder

Holds strong reference to a thread-local value.

Field Summary

Modifier and TypeField and Description
private final ThreadLocal<WeakReference<VicariousThreadLocal.Holder>>
local

Actual ThreadLocal implementation object.

private final ReferenceQueue<Object>
queue

Queue of Holders belonging to exited threads.

private volatile VicariousThreadLocal.Holder
strongRefs

Maintains a strong reference to value for each thread, so long as the Thread has not been collected.

private static final AtomicReferenceFieldUpdater<VicariousThreadLocal<T>, VicariousThreadLocal.Holder>
strongRefsUpdater

Compare-and-set of strongRefs.

private static final Object
UNINITIALISED

Object representing an uninitialised value.

private static final ThreadLocal<WeakReference<Thread>>
weakThread

Maps a unique WeakReference onto each Thread.

Constructor Summary

AccessConstructor and Description
public
VicariousThreadLocal()

Creates a new VicariousThreadLocal.

Method Summary

Modifier and TypeMethod and Description
private VicariousThreadLocal.Holder
createHolder()

Creates a new holder object, and registers it appropriately.

pack-priv static WeakReference<Thread>
currentThreadRef()

Returns a unique object representing the current thread.

public T
get()

Overrides java.lang.ThreadLocal.get.

Returns the value in the current thread's copy of this thread-local variable.
public void
poll()

Check if any strong references need should be removed due to thread exit.

public void
remove()

Overrides java.lang.ThreadLocal.remove.

Removes the current thread's value for this thread-local variable.
public void
set(T
the value to be stored in the current thread's copy of this thread-local.
value
)

Overrides java.lang.ThreadLocal.set.

Sets the current thread's copy of this thread-local variable to the specified value.
Inherited from java.lang.ThreadLocal:
initialValuewithInitial

Field Detail

localback to summary
private final ThreadLocal<WeakReference<VicariousThreadLocal.Holder>> local

Actual ThreadLocal implementation object.

queueback to summary
private final ReferenceQueue<Object> queue

Queue of Holders belonging to exited threads.

strongRefsback to summary
private volatile VicariousThreadLocal.Holder strongRefs

Maintains a strong reference to value for each thread, so long as the Thread has not been collected. Note, alive Threads strongly references the WeakReference<Thread> through weakThread.

strongRefsUpdaterback to summary
private static final AtomicReferenceFieldUpdater<VicariousThreadLocal<T>, VicariousThreadLocal.Holder> strongRefsUpdater

Compare-and-set of strongRefs.

UNINITIALISEDback to summary
private static final Object UNINITIALISED

Object representing an uninitialised value.

weakThreadback to summary
private static final ThreadLocal<WeakReference<Thread>> weakThread

Maps a unique WeakReference onto each Thread.

Constructor Detail

VicariousThreadLocalback to summary
public VicariousThreadLocal()

Creates a new VicariousThreadLocal.

Method Detail

createHolderback to summary
private VicariousThreadLocal.Holder createHolder()

Creates a new holder object, and registers it appropriately. Also polls for thread-exits.

currentThreadRefback to summary
pack-priv static WeakReference<Thread> currentThreadRef()

Returns a unique object representing the current thread. Although we use a weak-reference to the thread, we could use practically anything that does not reference our class-loader.

getback to summary
public T get()

Overrides java.lang.ThreadLocal.get.

Doc from java.lang.ThreadLocal.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:T

the current thread's value of this thread-local

Annotations
@SuppressWarnings:unchecked
@Override
pollback to summary
public void poll()

Check if any strong references need should be removed due to thread exit.

removeback to summary
public void remove()

Overrides java.lang.ThreadLocal.remove.

Doc from java.lang.ThreadLocal.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.

Annotations
@Override
setback to summary
public void set(T value)

Overrides java.lang.ThreadLocal.set.

Doc from java.lang.ThreadLocal.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 the initialValue method to set the values of thread-locals.

Parameters
value:T

the value to be stored in the current thread's copy of this thread-local.

Annotations
@Override
net.sf.ehcache.util.lang back to summary

private Class VicariousThreadLocal.Holder

extends WeakReference<Object>
Class Inheritance

Holds strong reference to a thread-local value. The WeakReference is to a thread-local representing the current thread.

Field Summary

Modifier and TypeField and Description
pack-priv VicariousThreadLocal.Holder
next

Hides java.lang.ref.Reference.next.

Next holder in chain for this thread-local.
pack-priv Object
value

Current thread-local value.

Constructor Summary

AccessConstructor and Description
pack-priv
Holder(ReferenceQueue<Object> queue)

Construct a new holder for the current thread.

Field Detail

nextback to summary
pack-priv VicariousThreadLocal.Holder next

Hides java.lang.ref.Reference.next.

Next holder in chain for this thread-local.

valueback to summary
pack-priv Object value

Current thread-local value. UNINITIALISED represents an uninitialised value.

Constructor Detail

Holderback to summary
pack-priv Holder(ReferenceQueue<Object> queue)

Construct a new holder for the current thread.