public class VicariousThreadLocal<T>
extends java.lang.ThreadLocal<T>
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:ThreadLocal
Modifier and Type | Class and Description |
---|---|
private static class |
VicariousThreadLocal.Holder
Holds strong reference to a thread-local value.
|
Modifier and Type | Field and Description |
---|---|
private java.lang.ThreadLocal<java.lang.ref.WeakReference<VicariousThreadLocal.Holder>> |
local
Actual ThreadLocal implementation object.
|
private java.lang.ref.ReferenceQueue<java.lang.Object> |
queue
Queue of Holders belonging to exited threads.
|
private VicariousThreadLocal.Holder |
strongRefs
Maintains a strong reference to value for each thread,
so long as the Thread has not been collected.
|
private static java.util.concurrent.atomic.AtomicReferenceFieldUpdater<VicariousThreadLocal,VicariousThreadLocal.Holder> |
strongRefsUpdater
Compare-and-set of
strongRefs . |
private static java.lang.Object |
UNINITIALISED
Object representing an uninitialised value.
|
private static java.lang.ThreadLocal<java.lang.ref.WeakReference<java.lang.Thread>> |
weakThread
Maps a unique WeakReference onto each Thread.
|
Constructor and Description |
---|
VicariousThreadLocal()
Creates a new
VicariousThreadLocal . |
Modifier and Type | Method and Description |
---|---|
private VicariousThreadLocal.Holder |
createHolder()
Creates a new holder object, and registers it appropriately.
|
(package private) static java.lang.ref.WeakReference<java.lang.Thread> |
currentThreadRef()
Returns a unique object representing the current thread.
|
T |
get() |
void |
poll()
Check if any strong references need should be removed due to thread exit.
|
void |
remove() |
void |
set(T value) |
private static final java.lang.ThreadLocal<java.lang.ref.WeakReference<java.lang.Thread>> weakThread
private static final java.lang.Object UNINITIALISED
private final java.lang.ThreadLocal<java.lang.ref.WeakReference<VicariousThreadLocal.Holder>> local
private volatile VicariousThreadLocal.Holder strongRefs
private static final java.util.concurrent.atomic.AtomicReferenceFieldUpdater<VicariousThreadLocal,VicariousThreadLocal.Holder> strongRefsUpdater
strongRefs
.private final java.lang.ref.ReferenceQueue<java.lang.Object> queue
public VicariousThreadLocal()
VicariousThreadLocal
.static java.lang.ref.WeakReference<java.lang.Thread> currentThreadRef()
private VicariousThreadLocal.Holder createHolder()
public void remove()
remove
in class java.lang.ThreadLocal<T>
public void poll()