Class ThreadLocalContext

java.lang.Object
io.fluxzero.sdk.common.ThreadLocalContext

public final class ThreadLocalContext extends Object
Shared, opt-in context for thread-local values that belong to the same logical request.

A participating value is created using create(). Calling capture() takes one snapshot of all participating values, which can subsequently be activated around work on another thread. Activation is nested and restores the context that was previously present on that thread.

Participating values remain regular thread locals on the hot path. The shared registry is only consulted when a context is captured or activated, so merely entering and leaving a request context does not allocate a map. Removing a participating value removes its thread-local value in the usual way; once the final child is removed, there is therefore no request context left on that thread.

  • Method Details

    • create

      public static <T> ThreadLocal<T> create()
      Creates a thread local whose value participates in this shared context.

      The returned holder is an ordinary ThreadLocal; participation only adds work when a snapshot is captured or activated.

    • capture

      public static ThreadLocalContext.Snapshot capture()
      Returns an immutable snapshot of every context value active on the current thread.