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 final ThreadLocal | local
Actual ThreadLocal implementation object. |
| private final ReferenceQueue | queue
Queue of Holders belonging to exited threads. |
| private volatile VicariousThreadLocal. | strongRefs
Maintains a strong reference to value for each thread, so long as the Thread has not been collected. |
| private static final AtomicReferenceFieldUpdater | strongRefsUpdater
Compare-and-set of |
| private static final Object | UNINITIALISED
Object representing an uninitialised value. |
| private static final ThreadLocal | weakThread
Maps a unique WeakReference onto each Thread. |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| private VicariousThreadLocal. | |
| pack-priv static WeakReference | |
| public T | get()
Overrides java. |
| public void | |
| public void | remove()
Overrides java. |
| public void | set(T
the value to be stored in the current thread's copy of
this thread-local. value)Overrides java. |
| local | back to summary |
|---|---|
| private final ThreadLocal<WeakReference<VicariousThreadLocal. Actual ThreadLocal implementation object. | |
| queue | back to summary |
|---|---|
| private final ReferenceQueue<Object> queue Queue of Holders belonging to exited threads. | |
| strongRefs | back to summary |
|---|---|
| private volatile VicariousThreadLocal. 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. | |
| strongRefsUpdater | back to summary |
|---|---|
| private static final AtomicReferenceFieldUpdater<VicariousThreadLocal<T>, VicariousThreadLocal. Compare-and-set of | |
| UNINITIALISED | back to summary |
|---|---|
| private static final Object UNINITIALISED Object representing an uninitialised value. | |
| weakThread | back to summary |
|---|---|
| private static final ThreadLocal<WeakReference<Thread>> weakThread Maps a unique WeakReference onto each Thread. | |
| VicariousThreadLocal | back to summary |
|---|---|
| public VicariousThreadLocal() Creates a new | |
| createHolder | back to summary |
|---|---|
| private VicariousThreadLocal. Creates a new holder object, and registers it appropriately. Also polls for thread-exits. | |
| currentThreadRef | back to summary |
|---|---|
| pack-priv static WeakReference 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. | |
| get | back to summary |
|---|---|
| public T get() Overrides java. Doc from java. 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
| |
| poll | back to summary |
|---|---|
| public void poll() Check if any strong references need should be removed due to thread exit. | |
| remove | back to summary |
|---|---|
| public void remove() Overrides java. Doc from java. 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
| |
| set | back to summary |
|---|---|
| public void set(T value) Overrides java. Doc from java. 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
| |
| Modifier and Type | Field and Description |
|---|---|
| pack-priv VicariousThreadLocal. | next
Hides java. |
| pack-priv Object | value
Current thread-local value. |
| Access | Constructor and Description |
|---|---|
| pack-priv |
| next | back to summary |
|---|---|
| pack-priv VicariousThreadLocal. Hides java. Next holder in chain for this thread-local. | |
| value | back to summary |
|---|---|
| pack-priv Object value Current thread-local value.
| |
| Holder | back to summary |
|---|---|
| pack-priv Holder(ReferenceQueue<Object> queue) Construct a new holder for the current thread. | |