当前位置:网站首页>ThansmittableThreadLocal详解
ThansmittableThreadLocal详解
2022-06-12 19:03:00 【靖节先生】
ThansmittableThreadLocal详解
1. ThansmittableThreadLocal简介
ThreadLocal只能保存当前线程的信息,不能实现父子线程的继承。
InheritableThreadLocal,确实InheritableThreadLocal能够实现父子线程间传递本地变量,但是你的程序如果采用线程池,则存在着线程复用的情况,这时就不一定能够实现父子线程间传递了,因为在线程在线程池中的存在不是每次使用都会进行创建,InheritableThreadlocal是在线程初始化时intertableThreadLocals=true才会进行拷贝传递。
ThansmittableThreadLocal,可以复用线程池内的线程,优雅的实现父子线程的数据传递,应用场景很多,企业中应用也比较广泛。
2. InheritableThreadLocal 的问题
代码中创建了一个单一线程池,循环异步调用,打印一下username,由于核心线程数是1,势必存在线程的复用。这里并没有实现父子线程间的变量传递,这也就是InheritableThreadLocal 的局限性。
@Test
public void test() throws Exception {
//单一线程池
ExecutorService executorService = Executors.newSingleThreadExecutor();
//InheritableThreadLocal存储
InheritableThreadLocal<String> username = new InheritableThreadLocal<>();
for (int i = 0; i < 10; i++) {
username.set("InheritableThreadLocal 的问题—"+i);
Thread.sleep(3000);
CompletableFuture.runAsync(()-> System.out.println(username.get()),executorService);
}
}
3. TransmittableThreadLocal 问题
TransmittableThreadLocal(TTL):在使用线程池等会池化复用线程的执行组件情况下,提供ThreadLocal值的传递功能,解决异步执行时上下文传递的问题。整个TransmittableThreadLocal库的核心功能(用户API与框架/中间件的集成API、线程池ExecutorService/ForkJoinPool/TimerTask及其线程工厂的Wrapper)。
引入依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>transmittable-thread-local</artifactId>
</dependency
代码实现
@Test
public void test() throws Exception {
//单一线程池
ExecutorService executorService = Executors.newSingleThreadExecutor();
//需要注意的是需要使用TtlExecutors对线程池进行包装,代码如下:
executorService=TtlExecutors.getTtlExecutorService(executorService);
//TransmittableThreadLocal创建
TransmittableThreadLocal<String> username = new TransmittableThreadLocal<>();
for (int i = 0; i < 10; i++) {
username.set("transmittable-thread-local—"+i);
Thread.sleep(3000);
CompletableFuture.runAsync(()-> System.out.println(username.get()),executorService);
}
}
可以看到已经能够实现了线程池中的父子线程的数据传递。在每次调用任务的时,都会将当前的主线程的TTL数据copy到子线程里面,执行完成后,再清除掉。同时子线程里面的修改回到主线程时其实并没有生效。这样可以保证每次任务执行的时候都是互不干涉。
4. ThansmittableThreadLocal原理分析
从定义来看,TransimittableThreadLocal继承于InheritableThreadLocal,并实现TtlCopier接口,它里面只有一个copy方法。所以主要是对InheritableThreadLocal的扩展。
public class TransmittableThreadLocal<T> extends InheritableThreadLocal<T> implements TtlCopier<T>
在TransimittableThreadLocal中添加holder属性。这个属性的作用就是被标记为具备线程传递资格的对象都会被添加到这个对象中。
要标记一个类,比较容易想到的方式,就是给这个类新增一个Type字段,还有一个方法就是将具备这种类型的的对象都添加到一个静态全局集合中。之后使用时,这个集合里的所有值都具备这个标记。
// 1. holder本身是一个InheritableThreadLocal对象
// 2. 这个holder对象的value是WeakHashMap<TransmittableThreadLocal<Object>, ?>
// 2.1 WeekHashMap的value总是null,且不可能被使用。
// 2.2 WeekHasshMap支持value=null
private static InheritableThreadLocal<WeakHashMap<TransmittableThreadLocal<Object>, ?>> holder = new InheritableThreadLocal<WeakHashMap<TransmittableThreadLocal<Object>, ?>>() {
@Override
protected WeakHashMap<TransmittableThreadLocal<Object>, ?> initialValue() {
return new WeakHashMap<TransmittableThreadLocal<Object>, Object>();
}
/** * 重写了childValue方法,实现上直接将父线程的属性作为子线程的本地变量对象。 */
@Override
protected WeakHashMap<TransmittableThreadLocal<Object>, ?> childValue(WeakHashMap<TransmittableThreadLocal<Object>, ?> parentValue) {
return new WeakHashMap<TransmittableThreadLocal<Object>, Object>(parentValue);
}
};
应用代码是通过TtlExecutors工具类对线程池对象进行包装。工具类只是简单的判断,输入的线程池是否已经被包装过、非空校验等,然后返回包装类ExecutorServiceTtlWrapper。根据不同的线程池类型,有不同和的包装类。
@Nullable
public static ExecutorService getTtlExecutorService(@Nullable ExecutorService executorService) {
if (TtlAgent.isTtlAgentLoaded() || executorService == null || executorService instanceof TtlEnhanced) {
return executorService;
}
return new ExecutorServiceTtlWrapper(executorService);
}
进入包装类ExecutorServiceTtlWrapper。可以注意到不论是通过ExecutorServiceTtlWrapper#submit方法或者是ExecutorTtlWrapper#execute方法,都会将线程对象包装成TtlCallable或者TtlRunnable,用于在真正执行run方法前做一些业务逻辑。
/** * 在ExecutorServiceTtlWrapper实现submit方法 */
@NonNull
@Override
public <T> Future<T> submit(@NonNull Callable<T> task) {
return executorService.submit(TtlCallable.get(task));
}
/** * 在ExecutorTtlWrapper实现execute方法 */
@Override
public void execute(@NonNull Runnable command) {
executor.execute(TtlRunnable.get(command));
}
所以,重点的核心逻辑应该是在TtlCallable#call()或者TtlRunnable#run()中。以下以TtlCallable为例,TtlRunnable同理类似。在分析call()方法之前,先看一个类Transmitter
public static class Transmitter {
/** * 捕获当前线程中的是所有TransimittableThreadLocal和注册ThreadLocal的值。 */
@NonNull
public static Object capture() {
return new Snapshot(captureTtlValues(), captureThreadLocalValues());
}
/** * 捕获TransimittableThreadLocal的值,将holder中的所有值都添加到HashMap后返回。 */
private static HashMap<TransmittableThreadLocal<Object>, Object> captureTtlValues() {
HashMap<TransmittableThreadLocal<Object>, Object> ttl2Value =
new HashMap<TransmittableThreadLocal<Object>, Object>();
for (TransmittableThreadLocal<Object> threadLocal : holder.get().keySet()) {
ttl2Value.put(threadLocal, threadLocal.copyValue());
}
return ttl2Value;
}
/** * 捕获注册的ThreadLocal的值,也就是原本线程中的ThreadLocal,可以注册到TTL中,在 * 进行线程池本地变量传递时也会被传递。 */
private static HashMap<ThreadLocal<Object>, Object> captureThreadLocalValues() {
final HashMap<ThreadLocal<Object>, Object> threadLocal2Value =
new HashMap<ThreadLocal<Object>, Object>();
for(Map.Entry<ThreadLocal<Object>,TtlCopier<Object>>entry:threadLocalHolder.entrySet()){
final ThreadLocal<Object> threadLocal = entry.getKey();
final TtlCopier<Object> copier = entry.getValue();
threadLocal2Value.put(threadLocal, copier.copy(threadLocal.get()));
}
return threadLocal2Value;
}
/** * 将捕获到的本地变量进行替换子线程的本地变量,并且返回子线程现有的本地变量副本backup。 * 用于在执行run/call方法之后,将本地变量副本恢复。 */
@NonNull
public static Object replay(@NonNull Object captured) {
final Snapshot capturedSnapshot = (Snapshot) captured;
return new Snapshot(replayTtlValues(capturedSnapshot.ttl2Value),
replayThreadLocalValues(capturedSnapshot.threadLocal2Value));
}
/** * 替换TransmittableThreadLocal */
@NonNull
private static HashMap<TransmittableThreadLocal<Object>, Object> replayTtlValues(@NonNull HashMap<TransmittableThreadLocal<Object>, Object> captured) {
// 创建副本backup
HashMap<TransmittableThreadLocal<Object>, Object> backup =
new HashMap<TransmittableThreadLocal<Object>, Object>();
for (final Iterator<TransmittableThreadLocal<Object>> iterator = holder.get().keySet().iterator(); iterator.hasNext(); ) {
TransmittableThreadLocal<Object> threadLocal = iterator.next();
// 对当前线程的本地变量进行副本拷贝
backup.put(threadLocal, threadLocal.get());
// 若出现调用线程中不存在某个线程变量,而线程池中线程有,则删除线程池中对应的本地变量
if (!captured.containsKey(threadLocal)) {
iterator.remove();
threadLocal.superRemove();
}
}
// 将捕获的TTL值打入线程池获取到的线程TTL中。
setTtlValuesTo(captured);
// 是一个扩展点,调用TTL的beforeExecute方法。默认实现为空
doExecuteCallback(true);
return backup;
}
private static HashMap<ThreadLocal<Object>, Object> replayThreadLocalValues(@NonNull HashMap<ThreadLocal<Object>, Object> captured) {
final HashMap<ThreadLocal<Object>, Object> backup =
new HashMap<ThreadLocal<Object>, Object>();
for (Map.Entry<ThreadLocal<Object>, Object> entry : captured.entrySet()) {
final ThreadLocal<Object> threadLocal = entry.getKey();
backup.put(threadLocal, threadLocal.get());
final Object value = entry.getValue();
if (value == threadLocalClearMark) threadLocal.remove();
else threadLocal.set(value);
}
return backup;
}
/** * 清除单线线程的所有TTL和TL,并返回清除之气的backup */
@NonNull
public static Object clear() {
final HashMap<TransmittableThreadLocal<Object>, Object> ttl2Value =
new HashMap<TransmittableThreadLocal<Object>, Object>();
final HashMap<ThreadLocal<Object>, Object> threadLocal2Value =
new HashMap<ThreadLocal<Object>, Object>();
for(Map.Entry<ThreadLocal<Object>,TtlCopier<Object>>entry:threadLocalHolder.entrySet()){
final ThreadLocal<Object> threadLocal = entry.getKey();
threadLocal2Value.put(threadLocal, threadLocalClearMark);
}
return replay(new Snapshot(ttl2Value, threadLocal2Value));
}
/** * 还原 */
public static void restore(@NonNull Object backup) {
final Snapshot backupSnapshot = (Snapshot) backup;
restoreTtlValues(backupSnapshot.ttl2Value);
restoreThreadLocalValues(backupSnapshot.threadLocal2Value);
}
private static void restoreTtlValues(@NonNull HashMap<TransmittableThreadLocal<Object>, Object> backup) {
// 扩展点,调用TTL的afterExecute
doExecuteCallback(false);
for (final Iterator<TransmittableThreadLocal<Object>> iterator = holder.get().keySet().iterator(); iterator.hasNext(); ) {
TransmittableThreadLocal<Object> threadLocal = iterator.next();
if (!backup.containsKey(threadLocal)) {
iterator.remove();
threadLocal.superRemove();
}
}
// 将本地变量恢复成备份版本
setTtlValuesTo(backup);
}
private static void setTtlValuesTo(@NonNull HashMap<TransmittableThreadLocal<Object>, Object> ttlValues) {
for (Map.Entry<TransmittableThreadLocal<Object>, Object> entry : ttlValues.entrySet()) {
TransmittableThreadLocal<Object> threadLocal = entry.getKey();
threadLocal.set(entry.getValue());
}
}
private static void restoreThreadLocalValues(@NonNull HashMap<ThreadLocal<Object>, Object> backup) {
for (Map.Entry<ThreadLocal<Object>, Object> entry : backup.entrySet()) {
final ThreadLocal<Object> threadLocal = entry.getKey();
threadLocal.set(entry.getValue());
}
}
/** * 快照类,保存TTL和TL */
private static class Snapshot {
final HashMap<TransmittableThreadLocal<Object>, Object> ttl2Value;
final HashMap<ThreadLocal<Object>, Object> threadLocal2Value;
private Snapshot(HashMap<TransmittableThreadLocal<Object>, Object> ttl2Value,
HashMap<ThreadLocal<Object>, Object> threadLocal2Value) {
this.ttl2Value = ttl2Value;
this.threadLocal2Value = threadLocal2Value;
}
}
进入TtlCallable#call()方法。
@Override
public V call() throws Exception {
Object captured = capturedRef.get();
if (captured == null || releaseTtlValueReferenceAfterCall &&
!capturedRef.compareAndSet(captured, null)) {
throw new IllegalStateException("TTL value reference is released after call!");
}
// 调用replay方法将捕获到的当前线程的本地变量,传递给线程池线程的本地变量,
// 并且获取到线程池线程覆盖之前的本地变量副本。
Object backup = replay(captured);
try {
// 线程方法调用
return callable.call();
} finally {
// 使用副本进行恢复。
restore(backup);
}
}
到这基本上线程池方式传递本地变量的核心代码已经大概看完了。总的来说在创建TtlCallable对象是,调用capture()方法捕获调用方的本地线程变量,在call()执行时,将捕获到的线程变量,替换到线程池所对应获取到的线程的本地变量中,并且在执行完成之后,将其本地变量恢复到调用之前。
边栏推荐
- How to download Vega in China
- CVPR 2022 Oral 大连理工提出SCI:快速、超强的低光照图像增强方法
- leetcode:5259. Calculate the total tax payable [simple simulation + see which range]
- 【矩阵论 & 图论】期末考试复习思维导图
- Leetcode 1049. 最后一块石头的重量 II
- Common troubleshooting tools and analysis artifacts are worth collecting
- Leetcode 416. Split equal sum subset
- 吃饭咯 干锅肥肠 + 掌中宝!
- Operational research optimization of meituan intelligent distribution system - Notes
- leetcode:6094. 公司命名【分组枚举 + 不能重复用set交集 + product笛卡儿积(repeat表示长度)】
猜你喜欢

Research Report on the overall scale, major manufacturers, major regions, products and application segments of lifeboats in the global market in 2022

Uniapp uses the Ali Icon

Exploration of a flexible injection scheme for istio sidecar
![leetcode:5289. Distribute cookies fairly [see data range + DFS pruning]](/img/be/820bfb3aaf23a397e65f96693770f2.png)
leetcode:5289. Distribute cookies fairly [see data range + DFS pruning]

WinCC7.5 SP1调整画面尺寸以适应显示分辨率的方法
![[blockbuster release] ant dynamic card, enabling the app home page to realize agile update](/img/65/5ed80090f4d0ee92b01888eb496528.jpg)
[blockbuster release] ant dynamic card, enabling the app home page to realize agile update

OpenGL shadow implementation (soft shadow)

How to download Vega in China

【历史上的今天】6 月 12 日:美国进入数字化电视时代;Mozilla 的最初开发者出生;3Com 和美国机器人公司合并

I was badly hurt by the eight part essay...
随机推荐
RT-Thread 模拟器 simulator 搭建 LVGL 的开发调试环境
Cookie & Session & kaptcha验证码
SCI Writing - Results
Research Report on current market situation and investment prospect of China's tobacco RFID industry 2022-2027
基于FPGA的VGA协议实现
攻防世界(web篇)---supersqli
什么是网络代理
Go init initialization function
leetcode:5289. 公平分发饼干【看数据范围 + dfs剪枝】
ISCC2022
收获满满的下午
Hugo blog building tutorial
Liunx deploy Seata (Nacos version)
[image denoising] image denoising based on anisotropic filtering with matlab code
YOLOX网络结构详解
Market development planning and investment prospect analysis report of Chinese government investment and financing platform 2022-2027
Report on the development status of China's asset appraisal industry and suggestions for future strategic planning 2022-2027
国内如何下载Vega
OpenGL shadow implementation (soft shadow)
I was badly hurt by the eight part essay...