需求: 主线程向异步线程传递用户token信息,
关于线程之间数据传递的几个关键对象,
先上例子
public static final ThreadLocal<String> context = new ThreadLocal<>();//
public static final ThreadLocal<String> context1 = new TransmittableThreadLocal<>();//
public static final ThreadLocal<String> context2 = new InheritableThreadLocal<>();
public static void main(String[] args) {
context.set("A");
context1.set("B");
context2.set("C");
CompletableFuture.runAsync(() -> {//ThreadLocal 线程之间隔离
String mm = context.get();
log.info("1: {}",mm);
});
CompletableFuture.runAsync(TtlRunnable.get(() -> { //TransmittableThreadLocal可以取到
String mm = context1.get();
log.info("2: {}",mm);
}));
CompletableFuture.runAsync(() -> { //取不到
String mm = context2.get();
log.info("3: {}",mm);
});
Thread thread = new Thread(() -> { // 子线程,正常可以取到
String mm = context2.get();
log.info("4: {}",mm);
});
thread.start();
}
[ForkJoinPool.commonPool-worker-1] INFO cn.me56.integration.stat.StatApiController - 1: null
[ForkJoinPool.commonPool-worker-2] INFO cn.me56.integration.stat.StatApiController - 2: A
[ForkJoinPool.commonPool-worker-1] INFO cn.me56.integration.stat.StatApiController - 3: null
[Thread-0] INFO cn.me56.integration.stat.StatApiController - 4: C
这里是InheritableThreadLocal使用子线程的例子:
public class InheritableThreadLocalDemo {
private static ThreadLocal<String> threadLocal = new InheritableThreadLocal<>();
public static void main(String[] args) {
threadLocal.set("mainThread");
System.out.println("value:"+threadLocal.get());
Thread thread = new Thread(new Runnable() { //子线程,可以取到
@Override
public void run() {
String value = threadLocal.get();
System.out.println("value:"+value);
}
});
thread.start();
}
}
value:mainThread
value:mainThread
TransmittableThreadLocal,简单使用方法
public static final ThreadLocal<String> context1 = new TransmittableThreadLocal<>();//
CompletableFuture.runAsync(TtlRunnable.get(() -> {
//do something...
}));
或者
CompletableFuture.supplyAsync(TtlWrappers.wrapSupplier(()-> {
//do something...
return "return value";
}) );
或者
java agent 方式
线程之间可以传递数据之后,再看下RequestContextHolder为空问题, 初步是想把request对象传给异步线程, 尝试之后觉得此方案不合理,没必要把整个request传进去
RequestContextHolder.setRequestAttributes(RequestContextHolder.getRequestAttributes(), true);
CompletableFuture.runAsync(() -> {
RequestContextHolder.setRequestAttributes(...);
//do something...
});
最终,因为业务需要完成的,是希望把长时间统计/查询接口,在系统资源有限的情况下,以时间换空间,拆分成多个子任务,所以这里线程之间传参仅仅只用了MDC.put/get方式简单实现了一下