Package org.jboss.weld.injection
Class ThreadLocalStack<T>
java.lang.Object
org.jboss.weld.injection.ThreadLocalStack<T>
- Direct Known Subclasses:
CurrentEventMetadata,CurrentInjectionPoint
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. 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 ThreadLocalStack.Stack.removeIfEmpty() call (see peek()) as an example.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceReference to a thread-local stack. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionpeek()pushConditionally(T item, boolean condition) Convenience method which only pushes something to stack if the condition evaluates to true.pushIfNotNull(T item) Convenience method which also accepts null values.
-
Constructor Details
-
ThreadLocalStack
public ThreadLocalStack()
-
-
Method Details
-
push
-
peek
-
pushConditionally
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.ThreadLocalStackReferenceobject is returned.ThreadLocalStack.ThreadLocalStackReference.pop()may be called on the returned object - it will not have any effect and always return null. -
pushIfNotNull
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.ThreadLocalStackReferenceobject is returned.ThreadLocalStack.ThreadLocalStackReference.pop()may be called on the returned object - it will not have any effect and always return null.
-