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 Summary

    Modifier and Type
    Method
    Description
    static <T,R> R
    callWithContext(String key, T value, Supplier<R> supplier)
    Executes in the specified context and returns a result 在指定上下文中执行并返回结果
    static void
    Clears all ThreadLocals for the current thread 清除当前线程的所有 ThreadLocal
    static boolean
    Checks if the ThreadLocal exists 检查 ThreadLocal 是否存在
    static <T> ThreadLocal<T>
    Creates a ThreadLocal 创建 ThreadLocal
    static <T> InheritableThreadLocal<T>
    Creates an InheritableThreadLocal 创建 InheritableThreadLocal
    static <T> InheritableThreadLocal<T>
    Creates an InheritableThreadLocal with initial value 创建带初始值的 InheritableThreadLocal
    static <T> ThreadLocal<T>
    Creates a ThreadLocal with initial value 创建带初始值的 ThreadLocal
    static <T> T
    get(String key)
    Gets the ThreadLocal value 获取 ThreadLocal 值
    static <T> T
    get(String key, T defaultValue)
    Gets the ThreadLocal value, returns default when absent 获取 ThreadLocal 值,不存在时返回默认值
    static <T> T
    getOrCompute(String key, Supplier<T> supplier)
    Gets the ThreadLocal value, computes via Supplier when absent 获取 ThreadLocal 值,不存在时通过 Supplier 计算
    static Set<String>
    Gets all ThreadLocal keys 获取所有 ThreadLocal 的 key
    static void
    Removes the specified ThreadLocal 移除指定 ThreadLocal
    static <T> void
    runWithContext(String key, T value, Runnable runnable)
    Executes in the specified context 在指定上下文中执行
    static <T> void
    set(String key, T value)
    Sets the ThreadLocal value 设置 ThreadLocal 值

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • get

      public static <T> T get(String key)
      Gets the ThreadLocal value 获取 ThreadLocal 值
    • get

      public static <T> T get(String key, T defaultValue)
      Gets the ThreadLocal value, returns default when absent 获取 ThreadLocal 值,不存在时返回默认值
    • getOrCompute

      public static <T> T getOrCompute(String key, Supplier<T> supplier)
      Gets the ThreadLocal value, computes via Supplier when absent 获取 ThreadLocal 值,不存在时通过 Supplier 计算
    • set

      public static <T> void set(String key, T value)
      Sets the ThreadLocal value 设置 ThreadLocal 值
    • remove

      public static void remove(String key)
      Removes the specified ThreadLocal 移除指定 ThreadLocal
    • clear

      public static void clear()
      Clears all ThreadLocals for the current thread 清除当前线程的所有 ThreadLocal
    • 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

      public static <T> void runWithContext(String key, T value, Runnable runnable)
      Executes in the specified context 在指定上下文中执行
    • callWithContext

      public static <T,R> R callWithContext(String key, T value, Supplier<R> supplier)
      Executes in the specified context and returns a result 在指定上下文中执行并返回结果
    • keys

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