Class ThreadLocalUtil

java.lang.Object
cloud.opencode.base.core.thread.ThreadLocalUtil

public final class ThreadLocalUtil extends Object
ThreadLocal Utility - Global named ThreadLocal management ThreadLocal 工具类 - 全局命名的 ThreadLocal 管理

Provides global named ThreadLocal storage with context execution support.

提供全局命名的 ThreadLocal 存储和上下文执行支持。

Features | 主要功能:

  • Named ThreadLocal get/set/remove - 命名 ThreadLocal 获取/设置/移除
  • Compute if absent (getOrCompute) - 不存在时计算
  • Context execution (runWithContext, callWithContext) - 上下文执行
  • InheritableThreadLocal creation - 可继承 ThreadLocal 创建

Usage Examples | 使用示例:

// Set/Get value - 设置/获取值
ThreadLocalUtil.set("userId", 123);
Integer userId = ThreadLocalUtil.get("userId");

// Run with context - 上下文执行
ThreadLocalUtil.runWithContext("tenant", "A", () -> {
    // code runs with tenant=A
});

Security | 安全性:

  • Thread-safe: Yes (ConcurrentHashMap) - 线程安全: 是
  • Null-safe: Yes - 空值安全: 是

Performance | 性能特性:

  • Time complexity: O(1) per get/set - 每次获取/设置 O(1)
  • Space complexity: O(1) per thread-local - 每个线程局部变量 O(1)
Since:
JDK 25, opencode-base-core V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • get

      @Deprecated(since="1.0.3") public static <T> T get(String key)
      Deprecated.
      Use ScopedValueUtil instead for virtual-thread-safe context propagation.
      Gets the ThreadLocal value 获取 ThreadLocal 值
    • get

      @Deprecated(since="1.0.3") public static <T> T get(String key, T defaultValue)
      Deprecated.
      Use ScopedValueUtil instead for virtual-thread-safe context propagation.
      Gets the ThreadLocal value, returns default when absent 获取 ThreadLocal 值,不存在时返回默认值
    • getOrCompute

      @Deprecated(since="1.0.3") public static <T> T getOrCompute(String key, Supplier<T> supplier)
      Deprecated.
      Use ScopedValueUtil instead for virtual-thread-safe context propagation.
      Gets the ThreadLocal value, computes via Supplier when absent 获取 ThreadLocal 值,不存在时通过 Supplier 计算
    • set

      @Deprecated(since="1.0.3") public static <T> void set(String key, T value)
      Deprecated.
      Use ScopedValueUtil instead for virtual-thread-safe context propagation.
      Sets the ThreadLocal value 设置 ThreadLocal 值
    • remove

      @Deprecated(since="1.0.3") public static void remove(String key)
      Deprecated.
      Use ScopedValueUtil instead for virtual-thread-safe context propagation.
      Removes the specified ThreadLocal value for the current thread 移除当前线程的指定 ThreadLocal 值
    • unregister

      public static void unregister(String name)
      Unregisters a named ThreadLocal from the global registry and removes its value. This allows the ThreadLocal instance to be garbage collected. 从全局注册表中注销指定名称的 ThreadLocal 并移除其值,使 ThreadLocal 实例可被 GC 回收。
      Parameters:
      name - the ThreadLocal name to unregister - 要注销的 ThreadLocal 名称
    • clear

      @Deprecated(since="1.0.3") public static void clear()
      Deprecated.
      Use ScopedValueUtil instead for virtual-thread-safe context propagation.
      Clears all ThreadLocal values for the current thread without affecting the global registry. Other threads' values remain accessible and can still be cleaned up later. 清除当前线程的所有 ThreadLocal 值,不影响全局注册表。 其他线程的值仍可访问,后续仍可清理。
    • unregisterAll

      public static void unregisterAll()
      Unregisters all ThreadLocal entries: removes values for the current thread and clears the global registry. 注销所有 ThreadLocal 条目:移除当前线程的值并清空全局注册表。

      WARNING: This is a global operation that affects ALL threads. After this call, ThreadLocal values set by other threads become unreachable through this utility and can no longer be cleaned up via clear() or remove(String). Typically only used during application shutdown.

      警告:这是一个全局操作,会影响所有线程。 调用后,其他线程通过本工具设置的 ThreadLocal 值将无法再通过 clear()remove(String) 清理。通常仅在应用关闭时使用。

    • contains

      public static boolean contains(String key)
      Checks if the ThreadLocal exists 检查 ThreadLocal 是否存在
    • create

      public static <T> ThreadLocal<T> create()
      Creates a ThreadLocal 创建 ThreadLocal
    • createWithInitial

      public static <T> ThreadLocal<T> createWithInitial(Supplier<T> supplier)
      Creates a ThreadLocal with initial value 创建带初始值的 ThreadLocal
    • createInheritable

      public static <T> InheritableThreadLocal<T> createInheritable()
      Creates an InheritableThreadLocal 创建 InheritableThreadLocal
    • createInheritableWithInitial

      public static <T> InheritableThreadLocal<T> createInheritableWithInitial(Supplier<T> supplier)
      Creates an InheritableThreadLocal with initial value 创建带初始值的 InheritableThreadLocal
    • runWithContext

      @Deprecated(since="1.0.3") public static <T> void runWithContext(String key, T value, Runnable runnable)
      Deprecated.
      Use ScopedValueUtil instead for virtual-thread-safe context propagation.
      Executes in the specified context 在指定上下文中执行
    • callWithContext

      @Deprecated(since="1.0.3") public static <T,R> R callWithContext(String key, T value, Supplier<R> supplier)
      Deprecated.
      Use ScopedValueUtil instead for virtual-thread-safe context propagation.
      Executes in the specified context and returns a result 在指定上下文中执行并返回结果
    • keys

      public static Set<String> keys()
      Gets all ThreadLocal keys 获取所有 ThreadLocal 的 key