diff --git a/src/main/java/com/github/phantomthief/scope/ScopeUtils.java b/src/main/java/com/github/phantomthief/scope/ScopeUtils.java index 903392e..8538e3d 100644 --- a/src/main/java/com/github/phantomthief/scope/ScopeUtils.java +++ b/src/main/java/com/github/phantomthief/scope/ScopeUtils.java @@ -14,6 +14,7 @@ import java.time.Duration; import java.util.Iterator; import java.util.Map.Entry; +import java.util.concurrent.Callable; import java.util.concurrent.ConcurrentMap; import java.util.concurrent.Executor; import java.util.concurrent.ExecutorService; @@ -70,6 +71,18 @@ private static Supplier wrapSupplierExistScope(@Nullable Scope scope, return () -> supplyWithExistScope(scope, supplier::get); } + /** + * 入参顺序调整,传入scope进入新线程 + * @param scope + * @param supplier + * @return + * @param + */ + private static Callable wrapCallableExistScope(@Nullable Scope scope, + @Nonnull Supplier supplier) { + return () -> supplyWithExistScope(scope, supplier::get); + } + public static void runAsyncWithCurrentScope(@Nonnull Runnable runnable, @Nonnull Executor executor) { executor.execute(wrapRunnableExistScope(getCurrentScope(), runnable)); @@ -84,13 +97,13 @@ public static ListenableFuture runAsyncWithCurrentScope(@Nonnull Runnable run @Nonnull public static Future supplyAsyncWithCurrentScope(@Nonnull Supplier supplier, @Nonnull ExecutorService executor) { - return executor.submit(() -> wrapSupplierExistScope(getCurrentScope(), supplier).get()); + return executor.submit(wrapCallableExistScope(getCurrentScope(), supplier)); } @Nonnull public static ListenableFuture supplyAsyncWithCurrentScope(@Nonnull Supplier supplier, @Nonnull ListeningExecutorService executor) { - return executor.submit(() -> wrapSupplierExistScope(getCurrentScope(), supplier).get()); + return executor.submit(wrapCallableExistScope(getCurrentScope(), supplier)); } /**