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)
RequestScopedCachefor cleaning up the thread local. IfRequestScopedCacheis active we do not remove the stack from thread local immediately when it becomes empty but defer this to the point whenRequestScopedCacheis cleaned up. Secondly, we reduce the number of ThreadLocal.get() accesses by returning aThreadLocalStack.ThreadLocalStackReferencewhich a client uses to pop a value. Lastly, theThreadLocalinstance is configured to set a new initial value by default. This is safe whenRequestScopedCacheis used but may lead toThreadLocalleak 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 interfaceThreadLocalStack.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 Tpeek()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.ThreadLocalStackReferenceobject 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.ThreadLocalStackReferenceobject is returned.ThreadLocalStack.ThreadLocalStackReference.pop()may be called on the returned object - it will not have any effect and always return null.
-
-