阿超
>
TransmittableThreadLocal坑
巨大建筑,总是由一木一石叠起来,我们何妨做做这一木一石呢?我时常做些零碎事,就是为此。——鲁迅
今天遇到一个坑,在使用TransimittableThreadLocal
(以下简称TTL
)时,用了一会儿,在子线程中突然获取不到TTL
在主线程中存储的变量了
翻了翻官方项目地址,发现我这里使用的CompletableFuture
没有指定线程池,后来指定使用TtlExecutors
包装的线程池就可以了
具体实现如下:
修改前:
1 2 3 4
| @SafeVarargs public static <T extends Model<T>, I> CompletableFuture<Map<I, List<T>>> asyncGroupBy(T entity, Collection<I> paramList, SFunction<T, I> sFunction, Consumer<T>... peeks) { return CompletableFuture.supplyAsync(() -> groupBy(entity, paramList, sFunction, peeks)); }
|
修改后:
1 2 3 4 5 6
| private static final Executor THREAD_POOL = TtlExecutors.getTtlExecutorService(ThreadUtil.newExecutor());
@SafeVarargs public static <T extends Model<T>, I> CompletableFuture<Map<I, List<T>>> asyncGroupBy(T entity, Collection<I> paramList, SFunction<T, I> sFunction, Consumer<T>... peeks) { return CompletableFuture.supplyAsync(() -> groupBy(entity, paramList, sFunction, peeks), THREAD_POOL); }
|
关于我写的这个groupBy
函数我之前博客也发过了,这里就不赘述了,这里是用hutool
的ThreadUtil.newExecutor()
创建的线程池