Class OwnerOrPerThreadState<S>

java.lang.Object
opennlp.tools.util.OwnerOrPerThreadState<S>
Type Parameters:
S - mutable state type (e.g. token probabilities + spans)

public final class OwnerOrPerThreadState<S> extends Object
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.

  • Constructor Details

    • OwnerOrPerThreadState

      public OwnerOrPerThreadState(Supplier<S> newState, Consumer<S> resetOwner)
      Parameters:
      newState - factory for one fresh state object (used for owner and for each thread)
      resetOwner - resets ownerState when the owner thread clears (must not clear other threads' state)
  • Method Details

    • get

      public S 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's ThreadLocal slot and, if this thread is the owner, resets owner state and releases ownership so a future single-thread user can claim it.