public class ThreadLocalStack<T> extends Object
RequestScopedCache
for cleaning up the thread local. If RequestScopedCache
is active
we do not remove the stack from thread local immediately when it becomes empty but defer this to the point when RequestScopedCache
is cleaned up.
Secondly, we reduce the number of ThreadLocal.get() accesses by returning a ThreadLocalStack.ThreadLocalStackReference
which a client uses to pop a value.
Lastly, the ThreadLocal
instance is configured to set a new initial value by default. This is safe when RequestScopedCache
is used
but may lead to ThreadLocal
leak when it is not. Therefore, special care needs to be take to guarantee that each ThreadLocal.get()
operation has a matching Stack#removeIfEmpty()
call (see peek()
) as an example.Modifier and Type | Class and Description |
---|---|
static interface |
ThreadLocalStack.ThreadLocalStackReference<T>
Reference to a thread-local stack.
|
Constructor and Description |
---|
ThreadLocalStack() |
Modifier and Type | Method and Description |
---|---|
T |
peek() |
ThreadLocalStack.ThreadLocalStackReference<T> |
push(T item) |
ThreadLocalStack.ThreadLocalStackReference<T> |
pushConditionally(T item,
boolean condition)
Convenience method which only pushes something to stack if the condition evaluates to true.
|
ThreadLocalStack.ThreadLocalStackReference<T> |
pushIfNotNull(T item)
Convenience method which also accepts null values.
|
public ThreadLocalStack.ThreadLocalStackReference<T> push(T item)
public T peek()
public ThreadLocalStack.ThreadLocalStackReference<T> pushConditionally(T item, boolean condition)
push(Object)
.
Otherwise, a special null ThreadLocalStack.ThreadLocalStackReference
object is returned. ThreadLocalStack.ThreadLocalStackReference.pop()
may
be called on the returned object - it will not have any effect and always return null.public ThreadLocalStack.ThreadLocalStackReference<T> pushIfNotNull(T item)
push(Object)
.
Otherwise, a special null ThreadLocalStack.ThreadLocalStackReference
object is returned. ThreadLocalStack.ThreadLocalStackReference.pop()
may
be called on the returned object - it will not have any effect and always return null.Copyright © 2015. All Rights Reserved.