Package opennlp.tools.util
Class OwnerOrPerThreadState<S>
java.lang.Object
opennlp.tools.util.OwnerOrPerThreadState<S>
- Type Parameters:
S- mutable state type (e.g. token probabilities + spans)
Routes mutable per-invocation state either to a single owner-thread field or to
ThreadLocal storage
once multiple threads use the same enclosing instance.
Why not always ThreadLocal? Frequently constructed components that are only used from one
thread would allocate or touch a ThreadLocal map on every access. The first thread uses a shared
ownerState until a second thread appears; then non-owner threads use lazily created per-thread
state.
The resetOwner callback is invoked when the owner thread calls
clearForCurrentThread() so owner fields can be restored to a clean state.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final Objectprivate booleanprivate static final longprivate final Sprivate final AtomicLongprivate final ThreadLocal<S> -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidRemoves this thread'sThreadLocalslot and, if this thread is the owner, resets owner state and releases ownership so a future single-thread user can claim it.get()Returns the state object for the calling thread (owner or per-thread slot).
-
Field Details
-
NO_OWNER_THREAD
private static final long NO_OWNER_THREAD- See Also:
-
threadState
-
ownerState
-
modeLock
-
ownerThreadId
-
multiThreaded
private volatile boolean multiThreaded -
resetOwner
-
-
Constructor Details
-
OwnerOrPerThreadState
- Parameters:
newState- factory for one fresh state object (used for owner and for each thread)resetOwner- resetsownerStatewhen the owner thread clears (must not clear other threads' state)
-
-
Method Details
-
get
Returns the state object for the calling thread (owner or per-thread slot).- Returns:
- mutable state for this thread
-
clearForCurrentThread
public void clearForCurrentThread()Removes this thread'sThreadLocalslot and, if this thread is the owner, resets owner state and releases ownership so a future single-thread user can claim it.
-