From 138c243d27e9c5ef488304591e14fef330329ef1 Mon Sep 17 00:00:00 2001 From: Toint <599818663@qq.com> Date: Fri, 24 Oct 2025 12:40:33 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=84=8F=E5=A4=96=E6=8F=90?= =?UTF-8?q?=E4=BA=A4=E7=9A=84ScopeValueUtil?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../hutool/v7/core/thread/ScopeValueUtil.java | 240 ------------------ 1 file changed, 240 deletions(-) delete mode 100644 hutool-core/src/main/java/cn/hutool/v7/core/thread/ScopeValueUtil.java diff --git a/hutool-core/src/main/java/cn/hutool/v7/core/thread/ScopeValueUtil.java b/hutool-core/src/main/java/cn/hutool/v7/core/thread/ScopeValueUtil.java deleted file mode 100644 index ede4ca5ea..000000000 --- a/hutool-core/src/main/java/cn/hutool/v7/core/thread/ScopeValueUtil.java +++ /dev/null @@ -1,240 +0,0 @@ -//package cn.hutool.v7.core.thread; -// -//import java.util.*; -//import java.util.concurrent.CompletableFuture; -//import java.util.concurrent.ExecutorService; -//import java.util.concurrent.Executors; -//import java.util.function.Supplier; -// -///** -// * ScopedValue 异步传播工具类 -// *

-// * 用于在异步任务(CompletableFuture)中传播 JDK 25+ 的 ScopedValue, -// * 解决 ScopedValue 在跨线程时值丢失的问题。 -// *

-// * -// *

使用示例:

-// *
{@code
-// * ScopedValue USER_ID = ScopedValue.newInstance();
-// *
-// * ScopedValue.where(USER_ID, "user123").run(() -> {
-// *     // 异步任务中自动传播 USER_ID
-// *     ScopeValueUtil.supplyAsync(USER_ID, () -> {
-// *         return "Processing for: " + USER_ID.get();
-// *     }).thenAccept(System.out::println);
-// * });
-// * }
-// * -// * @author Toint -// * @date 2025/10/21 -// */ -//public class ScopeValueUtil { -// -// /** -// * 虚拟线程执行器,用于执行异步任务 -// */ -// private static final ExecutorService executorService = Executors.newVirtualThreadPerTaskExecutor(); -// -// /** -// * 在异步任务中传播单个 ScopedValue(无返回值) -// *

-// * 捕获当前线程中指定 ScopedValue 的值,并在异步任务中恢复该值的绑定。 -// *

-// * -// * @param scopedValue 需要传播的 ScopedValue -// * @param runnable 异步执行的任务 -// * @param ScopedValue 的值类型 -// * @return CompletableFuture 异步任务的 Future 对象 -// */ -// public static CompletableFuture runAsync(ScopedValue scopedValue, Runnable runnable) { -// return runAsync(Collections.singletonList(scopedValue), runnable); -// } -// -// /** -// * 在异步任务中传播单个 ScopedValue(有返回值) -// *

-// * 捕获当前线程中指定 ScopedValue 的值,并在异步任务中恢复该值的绑定。 -// *

-// * -// * @param scopedValue 需要传播的 ScopedValue -// * @param supplier 异步执行的任务,返回结果 -// * @param ScopedValue 的值类型 -// * @param 异步任务的返回值类型 -// * @return CompletableFuture 异步任务的 Future 对象,包含任务执行结果 -// */ -// public static CompletableFuture supplyAsync(ScopedValue scopedValue, Supplier supplier) { -// return supplyAsync(Collections.singletonList(scopedValue), supplier); -// } -// -// /** -// * 在异步任务中传播多个 ScopedValue(无返回值) -// *

-// * 捕获当前线程中多个 ScopedValue 的值,并在异步任务中恢复这些值的绑定。 -// * 只有已绑定的 ScopedValue 会被传播,未绑定的会被忽略。 -// *

-// * -// * @param scopedValues 需要传播的 ScopedValue 列表 -// * @param runnable 异步执行的任务 -// * @param ScopedValue 的值类型 -// * @return CompletableFuture 异步任务的 Future 对象 -// */ -// public static CompletableFuture runAsync(List> scopedValues, Runnable runnable) { -// Map, V> scopedValueMap = captureContext(scopedValues); -// return runAsync(scopedValueMap, runnable); -// } -// -// /** -// * 在异步任务中传播多个 ScopedValue(有返回值) -// *

-// * 捕获当前线程中多个 ScopedValue 的值,并在异步任务中恢复这些值的绑定。 -// * 只有已绑定的 ScopedValue 会被传播,未绑定的会被忽略。 -// *

-// * -// * @param scopedValues 需要传播的 ScopedValue 列表 -// * @param supplier 异步执行的任务,返回结果 -// * @param ScopedValue 的值类型 -// * @param 异步任务的返回值类型 -// * @return CompletableFuture 异步任务的 Future 对象,包含任务执行结果 -// */ -// public static CompletableFuture supplyAsync(List> scopedValues, Supplier supplier) { -// Map, V> scopedValueMap = captureContext(scopedValues); -// return supplyAsync(scopedValueMap, supplier); -// } -// -// /** -// * 在异步任务中传播 ScopedValue 上下文(有返回值) -// *

-// * 根据提供的 ScopedValue 上下文映射,在异步任务中恢复这些值的绑定。 -// * 这是最底层的传播方法,其他 supplyAsync 方法最终都会调用此方法。 -// *

-// * -// * @param scopedValueMap ScopedValue 到其值的映射,表示需要传播的上下文 -// * @param supplier 异步执行的任务,返回结果 -// * @param ScopedValue 的值类型 -// * @param 异步任务的返回值类型 -// * @return CompletableFuture 异步任务的 Future 对象,包含任务执行结果 -// * @throws NullPointerException 如果 supplier 为 null -// */ -// public static CompletableFuture supplyAsync(Map, V> scopedValueMap, Supplier supplier) { -// Objects.requireNonNull(supplier, "supplier must not be null"); -// -// if (scopedValueMap == null || scopedValueMap.isEmpty()) { -// return CompletableFuture.supplyAsync(supplier, executorService); -// } -// -// return CompletableFuture.supplyAsync(() -> { -// ScopedValue.Carrier carrier = buildCarrier(scopedValueMap); -// return carrier.call(supplier::get); -// }, executorService); -// } -// -// /** -// * 在异步任务中传播 ScopedValue 上下文(无返回值) -// *

-// * 根据提供的 ScopedValue 上下文映射,在异步任务中恢复这些值的绑定。 -// * 这是最底层的传播方法,其他 runAsync 方法最终都会调用此方法。 -// *

-// * -// * @param scopedValueMap ScopedValue 到其值的映射,表示需要传播的上下文 -// * @param runnable 异步执行的任务 -// * @param ScopedValue 的值类型 -// * @return CompletableFuture 异步任务的 Future 对象 -// * @throws NullPointerException 如果 runnable 为 null -// */ -// public static CompletableFuture runAsync(Map, V> scopedValueMap, Runnable runnable) { -// Objects.requireNonNull(runnable, "runnable must not be null"); -// -// return supplyAsync(scopedValueMap, () -> { -// runnable.run(); -// return null; -// }); -// } -// -// /** -// * 构建 ScopedValue.Carrier 对象 -// *

-// * Carrier 用于在新的执行上下文中批量绑定多个 ScopedValue。 -// * 通过链式调用 where() 方法构建包含所有绑定的 Carrier。 -// *

-// * -// * @param scopedValueVMap ScopedValue 到其值的映射 -// * @param ScopedValue 的值类型 -// * @return ScopedValue.Carrier 对象,如果映射为空则返回 null -// */ -// public static ScopedValue.Carrier buildCarrier(Map, V> scopedValueVMap) { -// if (scopedValueVMap == null || scopedValueVMap.isEmpty()) return null; -// -// ScopedValue.Carrier carrier = null; -// for (Map.Entry, V> entry : scopedValueVMap.entrySet()) { -// if (carrier == null) { -// carrier = ScopedValue.where(entry.getKey(), entry.getValue()); -// } else { -// carrier = carrier.where(entry.getKey(), entry.getValue()); -// } -// } -// return carrier; -// } -// -// /** -// * 捕获当前线程中已绑定的 ScopedValue -// *

-// * 遍历指定的 ScopedValue 列表,提取所有在当前线程中已绑定的值, -// * 构建成 Map 以便后续在异步线程中恢复。未绑定或为 null 的 ScopedValue 会被忽略。 -// *

-// * -// * @param scopedValues 需要捕获的 ScopedValue 列表 -// * @param ScopedValue 的值类型 -// * @return Map, V> 包含已绑定值的映射,如果没有绑定值则返回空 Map -// */ -// public static Map, V> captureContext(List> scopedValues) { -// Map, V> scopedValueMap = new HashMap<>(); -// if (scopedValues != null && !scopedValues.isEmpty()) { -// scopedValues.forEach(scopedValue -> { -// if (scopedValue != null && scopedValue.isBound()) { -// scopedValueMap.put(scopedValue, scopedValue.get()); -// } -// }); -// } -// return scopedValueMap; -// } -// -// /** -// * 安全获取 ScopedValue 的绑定值 -// *

-// * 与 {@link ScopedValue#get()} 不同,此方法在 ScopedValue 未绑定时返回 null, -// * 而不是抛出 {@link NoSuchElementException} 异常。适用于需要容错处理的场景。 -// *

-// * -// *

使用场景:

-// *
    -// *
  • 不确定 ScopedValue 是否已绑定时
  • -// *
  • 需要优雅处理未绑定情况,避免异常
  • -// *
  • 可选的上下文传递场景
  • -// *
-// * -// *

使用示例:

-// *
{@code
-//     * ScopedValue USER_ID = ScopedValue.newInstance();
-//     *
-//     * // 安全获取,不会抛异常
-//     * String userId = ScopeValueUtil.getOrNull(USER_ID);
-//     * if (userId != null) {
-//     *     System.out.println("User ID: " + userId);
-//     * }
-//     * }
-// * -// * @param scopedValue 需要获取值的 ScopedValue,允许为 null -// * @param ScopedValue 的值类型 -// * @return 已绑定的值,如果未绑定或 scopedValue 为 null 则返回 null -// * @see ScopedValue#get() -// * @see ScopedValue#isBound() -// */ -// public static V getOrNull(ScopedValue scopedValue) { -// if (scopedValue != null && scopedValue.isBound()) { -// return scopedValue.get(); -// } else { -// return null; -// } -// } -// -//}