Class ThreadLocalUtil
java.lang.Object
cloud.opencode.base.core.thread.ThreadLocalUtil
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 TypeMethodDescriptionstatic <T,R> R callWithContext(String key, T value, Supplier<R> supplier) Executes in the specified context and returns a result 在指定上下文中执行并返回结果static voidclear()Clears all ThreadLocals for the current thread 清除当前线程的所有 ThreadLocalstatic booleanChecks if the ThreadLocal exists 检查 ThreadLocal 是否存在static <T> ThreadLocal<T> create()Creates a ThreadLocal 创建 ThreadLocalstatic <T> InheritableThreadLocal<T> Creates an InheritableThreadLocal 创建 InheritableThreadLocalstatic <T> InheritableThreadLocal<T> createInheritableWithInitial(Supplier<T> supplier) Creates an InheritableThreadLocal with initial value 创建带初始值的 InheritableThreadLocalstatic <T> ThreadLocal<T> createWithInitial(Supplier<T> supplier) Creates a ThreadLocal with initial value 创建带初始值的 ThreadLocalstatic <T> TGets the ThreadLocal value 获取 ThreadLocal 值static <T> TGets the ThreadLocal value, returns default when absent 获取 ThreadLocal 值,不存在时返回默认值static <T> TgetOrCompute(String key, Supplier<T> supplier) Gets the ThreadLocal value, computes via Supplier when absent 获取 ThreadLocal 值,不存在时通过 Supplier 计算keys()Gets all ThreadLocal keys 获取所有 ThreadLocal 的 keystatic voidRemoves the specified ThreadLocal 移除指定 ThreadLocalstatic <T> voidrunWithContext(String key, T value, Runnable runnable) Executes in the specified context 在指定上下文中执行static <T> voidSets the ThreadLocal value 设置 ThreadLocal 值
-
Method Details
-
get
Gets the ThreadLocal value 获取 ThreadLocal 值 -
get
Gets the ThreadLocal value, returns default when absent 获取 ThreadLocal 值,不存在时返回默认值 -
getOrCompute
-
set
Sets the ThreadLocal value 设置 ThreadLocal 值 -
remove
Removes the specified ThreadLocal 移除指定 ThreadLocal -
clear
public static void clear()Clears all ThreadLocals for the current thread 清除当前线程的所有 ThreadLocal -
contains
Checks if the ThreadLocal exists 检查 ThreadLocal 是否存在 -
create
Creates a ThreadLocal 创建 ThreadLocal -
createWithInitial
Creates a ThreadLocal with initial value 创建带初始值的 ThreadLocal -
createInheritable
Creates an InheritableThreadLocal 创建 InheritableThreadLocal -
createInheritableWithInitial
Creates an InheritableThreadLocal with initial value 创建带初始值的 InheritableThreadLocal -
runWithContext
-
callWithContext
-
keys
-