摘要:本文主要介绍如何使用transmittable-thread-local
来实现线程之间的值共享传递,解决多线程和线程池共享线程上下文变量问题。
功能
ransmittableThreadLocal
(TTL
):在使用线程池等会池化复用线程的执行组件情况下,提供ThreadLocal
值的传递功能,解决异步执行时上下文传递的问题。一个Java
标准库本应为框架/中间件设施开发提供的标配能力,本库功能聚焦 & 0依赖,支持Java 6~21
。
JDK
的InheritableThreadLocal
类可以完成父线程到子线程的值传递。但对于使用线程池等会池化复用线程的执行组件的情况,线程由线程池创建好,并且线程是池化起来反复使用的;这时父子线程关系的ThreadLocal
值传递已经没有意义,应用需要的实际上是把 任务提交给线程池时 的ThreadLocal
值传递到 任务执行时。
阿里的TransmittableThreadLocal
类继承并加强InheritableThreadLocal
类,解决上述的问题。
官方地址
使用案例
创建一个
TtlTest
的测试类
动态创建线程使用案例
该方式是每次都创建一个新的线程运行任务
测试方法
ini
@Test
void simple() throws Exception{
List<Thread> threads = new ArrayList<>();
for(int i=0;i<100;i++){
UserinfoContextUtils.setUserinfo(new Userinfo(Long.parseLong(i+""),String.valueOf(i)));
Thread thread = new Thread(() -> {
log.info(UserinfoContextUtils.getUserinfo().toString());
});
threads.add(thread);
}
threads.forEach(Thread::start);
Thread.sleep(5000L);
}
测试结果
ini
21:36:26.939 [Thread-40] INFO com.home.alibaba.ttl.TtlTest - Userinfo(id=39, name=39)
21:36:26.940 [Thread-4] INFO com.home.alibaba.ttl.TtlTest - Userinfo(id=3, name=3)
21:36:26.939 [Thread-2] INFO com.home.alibaba.ttl.TtlTest - Userinfo(id=1, name=1)
21:36:26.940 [Thread-9] INFO com.home.alibaba.ttl.TtlTest - Userinfo(id=8, name=8)
使用线程池,修饰传递的值
使用
TtlRunnable
和TtlCallable
来修饰传入线程池的Runnable
和Callable
。
测试方法
scss
@Test
void decorateValue() throws Exception{
ExecutorService executorService = Executors.newFixedThreadPool(2);
for(int i=0;i<10;i++){
UserinfoContextUtils.setUserinfo(new Userinfo(Long.parseLong(i+""),String.valueOf(i)));
Runnable runnable = new Runnable() {
@Override
public void run() {
log.info(UserinfoContextUtils.getUserinfo().toString());
}
};
Callable<Integer> callable = new Callable<Integer>() {
@Override
public Integer call() throws Exception {
log.info(UserinfoContextUtils.getUserinfo().toString());
return 1;
}
};
executorService.submit(TtlRunnable.get(runnable));
executorService.submit(TtlCallable.get(callable));
}
Thread.sleep(5000L);
}
测试结果
ini
21:38:09.692 [pool-1-thread-2] INFO com.home.alibaba.ttl.TtlTest - Userinfo(id=0, name=0)
21:38:09.692 [pool-1-thread-1] INFO com.home.alibaba.ttl.TtlTest - Userinfo(id=0, name=0)
21:38:09.694 [pool-1-thread-1] INFO com.home.alibaba.ttl.TtlTest - Userinfo(id=1, name=1)
21:38:09.694 [pool-1-thread-2] INFO com.home.alibaba.ttl.TtlTest - Userinfo(id=1, name=1)
21:38:09.694 [pool-1-thread-1] INFO com.home.alibaba.ttl.TtlTest - Userinfo(id=2, name=2)
21:38:09.694 [pool-1-thread-2] INFO com.home.alibaba.ttl.TtlTest - Userinfo(id=2, name=2)
修饰线程池
省去每次
Runnable
和Callable
传入线程池时的修饰,这个逻辑可以在线程池中完成。
通过工具类TtlExecutors
完成,有下面的方法:
getTtlExecutor
:修饰接口Executor
getTtlExecutorService
:修饰接口ExecutorService
getTtlScheduledExecutorService
:修饰接口ScheduledExecutorService
测试方法
ini
@Test
void decoratePool() throws Exception{
ExecutorService executorService = Executors.newFixedThreadPool(2);
executorService = TtlExecutors.getTtlExecutorService(executorService);
for(int i=0;i<10;i++){
UserinfoContextUtils.setUserinfo(new Userinfo(Long.parseLong(i+""),String.valueOf(i)));
Runnable runnable = new Runnable() {
@Override
public void run() {
log.info(UserinfoContextUtils.getUserinfo().toString());
}
};
Callable<Integer> callable = new Callable<Integer>() {
@Override
public Integer call() throws Exception {
log.info(UserinfoContextUtils.getUserinfo().toString());
return 1;
}
};
executorService.submit(runnable);
executorService.submit(callable);
}
Thread.sleep(5000L);
}
测试结果
ini
21:39:15.906 [pool-1-thread-1] INFO com.home.alibaba.ttl.TtlTest - Userinfo(id=0, name=0)
21:39:15.906 [pool-1-thread-2] INFO com.home.alibaba.ttl.TtlTest - Userinfo(id=0, name=0)
21:39:15.908 [pool-1-thread-1] INFO com.home.alibaba.ttl.TtlTest - Userinfo(id=1, name=1)
21:39:15.908 [pool-1-thread-2] INFO com.home.alibaba.ttl.TtlTest - Userinfo(id=1, name=1)
21:39:15.908 [pool-1-thread-1] INFO com.home.alibaba.ttl.TtlTest - Userinfo(id=2, name=2)
21:39:15.908 [pool-1-thread-2] INFO com.home.alibaba.ttl.TtlTest - Userinfo(id=2, name=2)
21:39:15.908 [pool-1-thread-1] INFO com.home.alibaba.ttl.TtlTest - Userinfo(id=3, name=3)
21:39:15.908 [pool-1-thread-2] INFO com.home.alibaba.ttl.TtlTest - Userinfo(id=3, name=3)
CompletableFuture
使用案例
使用该方式操作多线程的方式更加优雅
测试方法
ini
@Test
void decoratePoolCompletableFuture() throws Exception{
ExecutorService executorService = Executors.newFixedThreadPool(2);
executorService = TtlExecutors.getTtlExecutorService(executorService);
List<CompletableFuture<Void>> runAsyncs = new ArrayList<>();
List<CompletableFuture<Integer>> suppliedAsyncs = new ArrayList<>();
for(int i=0;i<10;i++){
UserinfoContextUtils.setUserinfo(new Userinfo(Long.parseLong(i+""),String.valueOf(i)));
Runnable runnable = new Runnable() {
@Override
public void run() {
log.info(UserinfoContextUtils.getUserinfo().toString());
}
};
CompletableFuture<Void> runAsync = CompletableFuture.runAsync(runnable, executorService);
runAsyncs.add(runAsync);
final int ii = i;
Supplier<Integer> supplier = new Supplier<Integer>() {
@Override
public Integer get() {
log.info(UserinfoContextUtils.getUserinfo().toString());
return ii;
}
};
CompletableFuture<Integer> suppliedAsync = CompletableFuture.supplyAsync(supplier,executorService);
suppliedAsyncs.add(suppliedAsync);
}
// 等待结果
CompletableFuture.allOf(runAsyncs.toArray(new CompletableFuture[]{})).isDone();
suppliedAsyncs.forEach(suppliedAsync -> {
try {
log.info(suppliedAsync.get().toString());
} catch (Exception e) {
log.error("get x",e);
}
});
}
测试结果
ini
21:51:27.592 [pool-1-thread-2] INFO com.home.alibaba.ttl.TtlTest - Userinfo(id=0, name=0)
21:51:27.592 [pool-1-thread-1] INFO com.home.alibaba.ttl.TtlTest - Userinfo(id=0, name=0)
21:51:27.594 [pool-1-thread-2] INFO com.home.alibaba.ttl.TtlTest - Userinfo(id=1, name=1)
21:51:27.594 [main] INFO com.home.alibaba.ttl.TtlTest - 0
21:51:27.594 [pool-1-thread-1] INFO com.home.alibaba.ttl.TtlTest - Userinfo(id=1, name=1)
21:51:27.595 [main] INFO com.home.alibaba.ttl.TtlTest - 1
21:51:27.595 [pool-1-thread-2] INFO com.home.alibaba.ttl.TtlTest - Userinfo(id=2, name=2)
21:51:27.595 [pool-1-thread-1] INFO com.home.alibaba.ttl.TtlTest - Userinfo(id=2, name=2)
21:51:27.595 [main] INFO com.home.alibaba.ttl.TtlTest - 2
21:51:27.595 [pool-1-thread-2] INFO com.home.alibaba.ttl.TtlTest - Userinfo(id=3, name=3)
21:51:27.595 [pool-1-thread-1] INFO com.home.alibaba.ttl.TtlTest - Userinfo(id=3, name=3)
21:51:27.595 [pool-1-thread-2] INFO com.home.alibaba.ttl.TtlTest - Userinfo(id=4, name=4)
21:51:27.595 [main] INFO com.home.alibaba.ttl.TtlTest - 3
21:51:27.595 [pool-1-thread-2] INFO com.home.alibaba.ttl.TtlTest - Userinfo(id=5, name=5)
21:51:27.595 [pool-1-thread-1] INFO com.home.alibaba.ttl.TtlTest - Userinfo(id=4, name=4)
21:51:27.595 [pool-1-thread-2] INFO com.home.alibaba.ttl.TtlTest - Userinfo(id=5, name=5)
21:51:27.595 [main] INFO com.home.alibaba.ttl.TtlTest - 4
21:51:27.595 [main] INFO com.home.alibaba.ttl.TtlTest - 5
21:51:27.595 [pool-1-thread-1] INFO com.home.alibaba.ttl.TtlTest - Userinfo(id=6, name=6)
21:51:27.595 [pool-1-thread-2] INFO com.home.alibaba.ttl.TtlTest - Userinfo(id=6, name=6)
21:51:27.595 [main] INFO com.home.alibaba.ttl.TtlTest - 6
21:51:27.595 [pool-1-thread-2] INFO com.home.alibaba.ttl.TtlTest - Userinfo(id=7, name=7)
21:51:27.595 [pool-1-thread-1] INFO com.home.alibaba.ttl.TtlTest - Userinfo(id=7, name=7)
21:51:27.595 [main] INFO com.home.alibaba.ttl.TtlTest - 7
21:51:27.595 [pool-1-thread-1] INFO com.home.alibaba.ttl.TtlTest - Userinfo(id=8, name=8)
21:51:27.595 [pool-1-thread-2] INFO com.home.alibaba.ttl.TtlTest - Userinfo(id=8, name=8)
21:51:27.595 [pool-1-thread-1] INFO com.home.alibaba.ttl.TtlTest - Userinfo(id=9, name=9)
21:51:27.595 [main] INFO com.home.alibaba.ttl.TtlTest - 8
21:51:27.595 [pool-1-thread-2] INFO com.home.alibaba.ttl.TtlTest - Userinfo(id=9, name=9)
21:51:27.595 [main] INFO com.home.alibaba.ttl.TtlTest - 9