Package org.jboss.weld.injection
Class ThreadLocalStack<T>
- java.lang.Object
-
- org.jboss.weld.injection.ThreadLocalStack<T>
-
- Direct Known Subclasses:
CurrentEventMetadata
,CurrentInjectionPoint
public class ThreadLocalStack<T> extends Object
A stack that is kept in thread-local. Two operations were identified to be expensive in micro benchmarks:- ThreadLocal.set()
- ThreadLocal.get() if the current value is null (because such get involves setting the initial value)
RequestScopedCache
for cleaning up the thread local. IfRequestScopedCache
is active we do not remove the stack from thread local immediately when it becomes empty but defer this to the point whenRequestScopedCache
is cleaned up. Secondly, we reduce the number of ThreadLocal.get() accesses by returning aThreadLocalStack.ThreadLocalStackReference
which a client uses to pop a value. Lastly, theThreadLocal
instance is configured to set a new initial value by default. This is safe whenRequestScopedCache
is used but may lead toThreadLocal
leak when it is not. Therefore, special care needs to be take to guarantee that eachThreadLocal.get()
operation has a matchingThreadLocalStack.Stack.removeIfEmpty()
call (seepeek()
) as an example.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
ThreadLocalStack.ThreadLocalStackReference<T>
Reference to a thread-local stack.
-
Constructor Summary
Constructors Constructor Description ThreadLocalStack()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method 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.
-
-
-
Method Detail
-
push
public ThreadLocalStack.ThreadLocalStackReference<T> push(T item)
-
peek
public T peek()
-
pushConditionally
public ThreadLocalStack.ThreadLocalStackReference<T> pushConditionally(T item, boolean condition)
Convenience method which only pushes something to stack if the condition evaluates to true. If the condition evaluates to true, this method behaves the same aspush(Object)
. Otherwise, a special nullThreadLocalStack.ThreadLocalStackReference
object is returned.ThreadLocalStack.ThreadLocalStackReference.pop()
may be called on the returned object - it will not have any effect and always return null.
-
pushIfNotNull
public ThreadLocalStack.ThreadLocalStackReference<T> pushIfNotNull(T item)
Convenience method which also accepts null values. If the given parameter is non-null, this method behaves the same aspush(Object)
. Otherwise, a special nullThreadLocalStack.ThreadLocalStackReference
object is returned.ThreadLocalStack.ThreadLocalStackReference.pop()
may be called on the returned object - it will not have any effect and always return null.
-
-