ThreadLocal的进化——TransmittableThreadLocal
上一篇文章中,我们谈到了 InheritableThreadLocal,它解决了 ThreadLocal 针对父子线程无法共享上下文
的问题。但我们可能听说过阿里的开源产品TransmittableThreadLocal
,那么它又是做什么的呢?
线程池中的共享
我们在多线程中,很少会直接 new 一个线程,更多的可能是利用线程池处理任务,那么利用 InheritableThreadLocal 可以将生成任务线程的上下文传递给执行任务的线程吗?废话不多说,直接上代码测试一下:
public class InheritableThreadLocalContext {
private static InheritableThreadLocal<Context> context = new InheritableThreadLocal<>();
static class Context {
String name;
int value;
}
public static void main(String[] args) {
// 固定线程池
ExecutorService executorService = Executors.newFixedThreadPool(4);
for (int i = 1; i <= 10; i++) {
int finalI = i;
new Thread(
() -> {
// 生成任务的线程对context进行赋值
Context contextMain = new Context();
contextMain.name = String.format("Thread%s name", finalI);
contextMain.value = finalI * 20;
InheritableThreadLocalContext.context.set(contextMain);
// 提交任务
for (int j = 1; j <= 10; j++) {
System.out.println("Thread" + finalI + " produce task " + (finalI * 20 + j));
executorService.execute(() -> {
// 执行任务的子线程
Context contextChild = InheritableThreadLocalContext.context.get();
System.out.println(Thread.currentThread().getName() + " execute task, name : " + contextChild.name + " value : " + contextChild.value);
});
}
}
).start();
}
}
}
我们希望的结果是,子线程输出的内容能够和父线程对应上。然而,实际的结果却出乎所料,我将结果整理一下:
Thread1 produce task 21
// 省略8行
Thread1 produce task 30
Thread2 produce task 41
// 省略8行
Thread2 produce task 50
pool-1-thread-1 execute task, name : Thread2 name value : 40
// 省略47行
pool-1-thread-1 execute task, name : Thread2 name value : 40
Thread3 produce task 61
// 省略8行
Thread3 produce task 70
Thread4 produce task 81
// 省略8行
Thread4 produce task 90
Thread5 produce task 101
// 省略8行
Thread5 produce task 110
Thread6 produce task 121
// 省略8行
Thread6 produce task 130
Thread7 produce task 141
// 省略8行
Thread7 produce task 150
pool-1-thread-2 execute task, name : Thread7 name value : 140
// 省略6行
pool-1-thread-2 execute task, name : Thread7 name value : 140
Thread8 produce task 161
// 省略8行
Thread8 produce task 170
Thread9 produce task 181
// 省略8行
Thread9 produce task 190
pool-1-thread-4 execute task, name : Thread9 name value : 180
pool-1-thread-4 execute task, name : Thread9 name value : 180
Thread10 produce task 201
// 省略8行
Thread10 produce task 210
pool-1-thread-3 execute task, name : Thread10 name value : 200
// 省略39行
pool-1-thread-3 execute task, name : Thread10 name value : 200
虽然生产总数和消费总数都是100,但是明显有的消费多了,有的消费少了。合理推测一下,应该是在主线程放进任务后,子线程才生成。为了验证这个猜想,将线程池用 ThreadPoolExecutor 生成,并在用子线程生成任务之前,先赋值 context 并开启所有线程:
public static void main(String[] args) {
// 固定线程池
ThreadPoolExecutor executorService = new ThreadPoolExecutor(
4,
4,
0L,
TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<>() );
// 在main线程中赋值
Context context = new Context();
context.name = "Thread0 name";
context.value = 0;
InheritableThreadLocalContext.context.set(context);
// 开启所有线程
executorService.prestartAllCoreThreads();
for (int i = 1; i <= 10; i++) {
int finalI = i;
new Thread(
() -> {
// 生成任务的线程对context进行赋值
Context contextMain = new Context();
contextMain.name = String.format("Thread%s name", finalI);
contextMain.value = finalI * 20;
InheritableThreadLocalContext.context.set(contextMain);
// 提交任务
for (int j = 1; j <= 10; j++) {
System.out.println("Thread" + finalI + " produce task " + (finalI * 20 + j));
executorService.execute(() -> {
// 执行任务的子线程
Context contextChild = InheritableThreadLocalContext.context.get();
System.out.println(Thread.currentThread().getName() + " execute task, name : " + contextChild.name + " value : " + contextChild.value);
});
}
}
).start();
}
}
结果不出所料,执行任务的线程输出的,都是最外面主线程设置的值。
那么我们该如何才能达到最初想要的效果呢?就是利用线程池执行任务时,如何能够让执行者线程能够获取调用者线程的 context 呢?
使用 TransmittableThreadLocal 解决
上面的问题主要是因为执行任务的线程是被线程池管理,可以被复用(可以称为池化复用
)。那复用了之后,如果还是依赖于父线程的 context,自然是有问题的,因为我们想要的效果是执行线程获取调用线程的 context,这时候就是TransmittableThreadLocal
出场了。
TransmittableThreadLocal 是阿里提供的工具类,其主要解决的就是上面遇到的问题。那么该如何使用呢?
首先,你需要引入相应的依赖:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>transmittable-thread-local</artifactId>
<version>2.11.0</version>
</dependency>
具体代码,就拿上文提到的情况,我们用 TransmittableThreadLocal 做一个改造:
public class TransmittableThreadLocalTest {
private static TransmittableThreadLocal<Context> context = new TransmittableThreadLocal<>();
static class Context {
String name;
int value;
}
public static void main(String[] args) {
// 固定线程池
ExecutorService executorService = Executors.newFixedThreadPool(4);
for (int i = 1; i <= 10; i++) {
int finalI = i;
new Thread(
() -> {
// 生成任务的线程对context进行赋值
Context contextMain = new Context();
contextMain.name = String.format("Thread%s name", finalI);
contextMain.value = finalI * 20;
TransmittableThreadLocalTest.context.set(contextMain);
// 提交任务
for (int j = 1; j <= 10; j++) {
System.out.println("Thread" + finalI + " produce task " + (finalI * 20 + j));
Runnable task = () -> {
// 执行任务的子线程
Context contextChild = TransmittableThreadLocalTest.context.get();
System.out.println(Thread.currentThread().getName() + " execute task, name : " + contextChild.name + " value : " + contextChild.value);
};
// 额外的处理,生成修饰了的对象ttlRunnable
Runnable ttlRunnable = TtlRunnable.get(task);
executorService.execute(ttlRunnable);
}
}
).start();
}
}
}
此时再次运行,就会发现执行线程运行时的输出内容是完全可以和调用线程对应上的了。当然了,我这种方式是修改了 Runnable 的写法,阿里也提供了线程池的写法,简单如下:
public static void main(String[] args) {
// 固定线程池
ExecutorService executorService = Executors.newFixedThreadPool(4);
// 额外的处理,生成修饰了的对象executorService
executorService = TtlExecutors.getTtlExecutorService(executorService);
ExecutorService finalExecutorService = executorService;
for (int i = 1; i <= 10; i++) {
int finalI = i;
new Thread(
() -> {
// 生成任务的线程对context进行赋值
Context contextMain = new Context();
contextMain.name = String.format("Thread%s name", finalI);
contextMain.value = finalI * 20;
TransmittableThreadLocalTest.context.set(contextMain);
// 提交任务
for (int j = 1; j <= 10; j++) {
System.out.println("Thread" + finalI + " produce task " + (finalI * 20 + j));
Runnable task = () -> {
// 执行任务的子线程
Context contextChild = TransmittableThreadLocalTest.context.get();
System.out.println(Thread.currentThread().getName() + " execute task, name : " + contextChild.name + " value : " + contextChild.value);
};
finalExecutorService.execute(task);
}
}
).start();
}
}
其实还有更加简单的写法,具体可以参考其github:https://github.com/alibaba/transmittable-thread-local
总结
其实两篇 ThreadLocal 升级文章的出现,都是因为周三听了一个部门关于 TTL 的分享会,也是介绍了 TransmittableThreadLocal,但因为携程商旅面临国际化的改动,当前的语种信息肯定是存储在线程的 context 中最方便,但涉及到线程传递的问题(因为会调用异步接口等等),所以自然就需要考虑这个了。性能方面的话,他们有做过测试,但我也只是一个听者,并没有具体使用过,大家也可以一起交流。
有兴趣的话可以访问我的博客或者关注我的公众号、头条号,说不定会有意外的惊喜。
公众号:健程之道
ThreadLocal的进化——TransmittableThreadLocal的更多相关文章
- ThreadLocal的进化——InheritableThreadLocal
之前有介绍过 ThreadLocal,JDK 后来针对此做了一个升级版本 InheritableThreadLocal,今天就来好好介绍下. 为什么要升级 首先我们来想想,为什么要升级?这就要说起 T ...
- ThreadLocal系列(三)-TransmittableThreadLocal的使用及原理解析
ThreadLocal系列(三)-TransmittableThreadLocal的使用及原理解析 上一篇:ThreadLocal系列(二)-InheritableThreadLocal的使用及原理解 ...
- 【原理】Java的ThreadLocal实现原理浅读
当前线程的值传递,ThreadLocal 通过ThreadLocal设值,在线程内可获取,即时获取值时在其它Class或其它Method. public class BasicUsage { priv ...
- ThreadLocal (三):为何TransmittableThreadLocal
一.示例 线程池内的线程并没有父子关系,所以不适合InheritableThreadLocal的使用场景 public class ThreadPoolInheritableThreadLocalDe ...
- 使用 transmittable-thread-local 组件解决 ThreadLocal 父子线程数据传递问题
在某个项目中,需要使用mybatis-plus多租户功能以便数据隔离,前端将租户id传到后端,后端通过拦截器将该租户id设置到ThreadLocal以便后续使用,代码大体上如下所示: ThreadLo ...
- ThreadLocal父子线程传递实现方案
介绍InheritableThreadLocal之前,假设对 ThreadLocal 已经有了一定的理解,比如基本概念,原理,如果没有,可以参考:ThreadLocal源码分析解密.在讲解之前我们先列 ...
- TransmittableThreadLocal 解决 线程池线程复用 无法复制 InheritableThreadLocal 的问题.
ThreadLoacl,InheritableThreadLocal,原理,以及配合线程池使用的一些坑 TransmittableThreadLocal 原理 之前为了能让InheritableThr ...
- ThreadLocal剧集(一)
总述 最近做了一个日志调用链路跟踪的项目,涉及到操作标识在线程和子线程,线程池以及远程调用之间的传递问题.最终采用了阿里开源的TransmittableThreadLocal插件(https: ...
- ThreadLocal系列(二)-InheritableThreadLocal的使用及原理解析
ThreadLocal系列之InheritableThreadLocal的使用及原理解析(源码基于java8) 上一篇:ThreadLocal系列(一)-ThreadLocal的使用及原理解析 下一篇 ...
随机推荐
- [LC]26题 Remove Duplicates from Sorted Array (删除排序数组中的重复项)(双指针法)(原地实现)
①中文题目 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成 ...
- nyoj 29-求转置矩阵问题 (行,列位置调换)
29-求转置矩阵问题 内存限制:64MB 时间限制:3000ms Special Judge: No accepted:8 submit:18 题目描述: 求一个三行三列的转置矩阵. 输入描述: 第一 ...
- 领扣(LeetCode)对称二叉树 个人题解
给定一个二叉树,检查它是否是镜像对称的. 例如,二叉树 [1,2,2,3,4,4,3] 是对称的. 1 / \ 2 2 / \ / \ 3 4 4 3 但是下面这个 [1,2,2,null,3,nul ...
- 领扣(LeetCode)转置矩阵 个人题解
给定一个矩阵 A, 返回 A 的转置矩阵. 矩阵的转置是指将矩阵的主对角线翻转,交换矩阵的行索引与列索引. 示例 1: 输入:[[1,2,3],[4,5,6],[7,8,9]] 输出:[[1,4,7] ...
- 如何使用Sping Data JPA更新局部字段
问题描述 在更新数据时,有时候我们只需要更新一部分字段,其他字段保持不变.Spring Data JPA并未提供现成的接口,直接使用save()更新会导致其他字段被Null覆盖掉. 解决办法 通常有两 ...
- 使用 Rsync 从 Windows 同步数据到 Linux
为什么要使用 rsync 从 Windows 到 linux 进行同步? 我们经常会面临这种的情况,项目使用 Windows 开发,最终部署在 Linux 上,但有时想要进行测试.维护.迭代版本时操作 ...
- H3C交换机console登录配置 v7
一.通过con口只需输入password登陆交换机. [H3C]user-interface aux 0 设置认证方式为密码验证方式 [H3C-ui-aux0] authentication-mode ...
- 【Luogu P2024&P1892】食物链&团伙(并查集拓展域)
Luogu P1892 Luogu P2024 这两道一眼看过去很容易发现可以用并查集来做--但是当我们仔细阅读题面后,会发现其实并没有那么简单. 我们知道并查集可以很轻松地维护具有传递性的信息,也就 ...
- 3 JAVA的基本变量类型
1. 数字 整数型 类型 字节数 范围 int 4 -2^31~ 2^31-1 short 2 -2^15~ 2^15 -1 long 8 -2^63 ~ 2^63 -1 byte 1 -2^ ...
- linuxshell编程之数组和字符串处理工具
数组:存放多个元素的连续内存空间. 声明数组:bash-4以后支持除默认的0,1,2……还可以自定义索引格式,此类数组称之为“关联数组” 声明索引数组:declare -a NAME 声明关联数组:d ...