当前位置: 首页 > news >正文

阿里巴巴TransmittableThreadLocal使用指南

前言

ThreadLocal在上下文的数据传输上非常的方便和简洁。工业实践中,比较常用的有三个,ThreadLocal、InheritableThreadLocal、TransmittableThreadLocal,那么他们三个之间有什么区别呢?

常见的三种ThreadLocal比较

ThreadLocalInheritableThreadLocalTransmittableThreadLocal
来源jdkjdk阿里开源
单线程数据传输支持支持支持
new线程数据传输不支持支持支持
线程池数据传输不支持部分支持【简单场景】支持

针对线程池的数据传输,InheritableThreadLocal仅仅能在一些简单场景下做到,下面就用一个案例来说明

先看下线程工厂的定义

package com.tml.mouseDemo.core.threadLocalDemo;import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;public class SimpleThreadFactory implements ThreadFactory {private static AtomicInteger atomicInteger = new AtomicInteger(1);@Overridepublic Thread newThread(Runnable r) {Thread t = new Thread(r);t.setName("tml-"+atomicInteger.getAndIncrement());return t;}
}

 InheritableThreadLocal部分支持的案例

package com.tml.mouseDemo.core.threadLocalDemo;import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;public class ThreadLocalDemo1 {private static ThreadLocal<String> threadLocal = new InheritableThreadLocal<>();private static ExecutorService service = Executors.newFixedThreadPool(2, new SimpleThreadFactory());public static void main(String[] args) throws InterruptedException {threadLocal.set("hello main");for (int i = 0; i < 2; i++) {service.execute(() -> {String s = threadLocal.get();ThreadUtils.printLog("get data " + s);});}//修改threadLocal中的值threadLocal.set("hello world");Thread.sleep(2000);for (int i = 0; i < 2; i++) {service.execute(() -> {String s = threadLocal.get();ThreadUtils.printLog("get data " + s);});}ThreadUtils.printLog("get data " + threadLocal.get());service.shutdown();}
}

运行结果如下 

2025-01-10 19:41:50 | INFO  | tml-1 | com.tml.mouseDemo.core.threadLocalDemo.ThreadUtils | get data hello main
2025-01-10 19:41:50 | INFO  | tml-2 | com.tml.mouseDemo.core.threadLocalDemo.ThreadUtils | get data hello main
2025-01-10 19:41:52 | INFO  | tml-2 | com.tml.mouseDemo.core.threadLocalDemo.ThreadUtils | get data hello main
2025-01-10 19:41:52 | INFO  | main | com.tml.mouseDemo.core.threadLocalDemo.ThreadUtils | get data hello world
2025-01-10 19:41:52 | INFO  | tml-1 | com.tml.mouseDemo.core.threadLocalDemo.ThreadUtils | get data hello main

从运行结果来看,前面的两次循环提交的任务,在子线程中确实是能正常的获取主线程设置的变量,即hello main,但是紧接着,我修改了主线程上绑定的变量为hello world,然后继续循环两次提交两个任务,这个时候子线程中获取的线程变量依然是hello main,这明显是与是期望不一致的。

从这个层面来讲,InheritableThreadLocal确实在线程池的层面支持不够友好,可以说仅支持部分简单场景。

根本原因就死线程池的池化机制,从上面的运行日志上也可以看出,提交了4个任务执行的线程依然是两个。线程池中的线程是复用的,InheritableThreadLocal是在创建子线程的时候,会将主线程上绑定的数据拷贝过来,如果我不创建新的线程,但是主线程上绑定的数据改变了呢?那我依然还是读取到之前拷贝的数据。

这个就是InheritableThreadLocal的短板。针对这个问题,阿里开源的TransmittableThreadLocal就能顺利丝滑的解决这个问题。

TransmittableThreadLocal实践

maven依赖

<dependency><groupId>com.alibaba</groupId><artifactId>transmittable-thread-local</artifactId><version>2.12.4</version>
</dependency>

装饰Runnable任务

直接看代码

package com.tml.mouseDemo.core.threadLocalDemo;import com.alibaba.ttl.TransmittableThreadLocal;
import com.alibaba.ttl.TtlRunnable;import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;public class ThreadLocalDemo1 {private static ThreadLocal<String> threadLocal = new TransmittableThreadLocal<>();private static ExecutorService service = Executors.newFixedThreadPool(2, new SimpleThreadFactory());public static void main(String[] args) throws InterruptedException {threadLocal.set("hello main");for (int i = 0; i < 2; i++) {service.execute(TtlRunnable.get(() -> {String s = threadLocal.get();ThreadUtils.printLog("get data " + s);}));}//修改threadLocal中的值threadLocal.set("hello world");Thread.sleep(2000);for (int i = 0; i < 2; i++) {service.execute(TtlRunnable.get(() -> {String s = threadLocal.get();ThreadUtils.printLog("get data " + s);}));}ThreadUtils.printLog("get data " + threadLocal.get());service.shutdown();}
}

运行结果如下:

 2025-01-10 19:57:03 | INFO  | tml-2 | com.tml.mouseDemo.core.threadLocalDemo.ThreadUtils | get data hello main
2025-01-10 19:57:03 | INFO  | tml-1 | com.tml.mouseDemo.core.threadLocalDemo.ThreadUtils | get data hello main
2025-01-10 19:57:05 | INFO  | tml-1 | com.tml.mouseDemo.core.threadLocalDemo.ThreadUtils | get data hello world
2025-01-10 19:57:05 | INFO  | main | com.tml.mouseDemo.core.threadLocalDemo.ThreadUtils | get data hello world
2025-01-10 19:57:05 | INFO  | tml-2 | com.tml.mouseDemo.core.threadLocalDemo.ThreadUtils | get data hello world

与第一个案例的差异点在于,使用了

public static TtlRunnable get(@Nullable Runnable runnable) {return get(runnable, false, false);
}

 来增强了了Runnable任务,执行的结果也是符合预期。

装饰线程池

直接看代码

package com.tml.mouseDemo.core.threadLocalDemo;import com.alibaba.ttl.TransmittableThreadLocal;
import com.alibaba.ttl.TtlRunnable;
import com.alibaba.ttl.threadpool.TtlExecutors;import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;public class ThreadLocalDemo1 {private static ThreadLocal<String> threadLocal = new TransmittableThreadLocal<>();private static ExecutorService service = Executors.newFixedThreadPool(2, new SimpleThreadFactory());private static ExecutorService ttlExecutorService = TtlExecutors.getTtlExecutorService(service);public static void main(String[] args) throws InterruptedException {threadLocal.set("hello main");for (int i = 0; i < 2; i++) {ttlExecutorService.execute(() -> {String s = threadLocal.get();ThreadUtils.printLog("get data " + s);});}//修改threadLocal中的值threadLocal.set("hello world");Thread.sleep(2000);for (int i = 0; i < 2; i++) {ttlExecutorService.execute(() -> {String s = threadLocal.get();ThreadUtils.printLog("get data " + s);});}ThreadUtils.printLog("get data " + threadLocal.get());service.shutdown();}
}

 运行结果如下

2025-01-10 20:05:05 | INFO  | tml-2 | com.tml.mouseDemo.core.threadLocalDemo.ThreadUtils | get data hello main
2025-01-10 20:05:05 | INFO  | tml-1 | com.tml.mouseDemo.core.threadLocalDemo.ThreadUtils | get data hello main
2025-01-10 20:05:07 | INFO  | tml-2 | com.tml.mouseDemo.core.threadLocalDemo.ThreadUtils | get data hello world
2025-01-10 20:05:07 | INFO  | main | com.tml.mouseDemo.core.threadLocalDemo.ThreadUtils | get data hello world
2025-01-10 20:05:07 | INFO  | tml-1 | com.tml.mouseDemo.core.threadLocalDemo.ThreadUtils | get data hello world

与上一个案例的差异点在于,这里没有包装Runnable任务,而是包装了线程池,使用了

public static ExecutorService getTtlExecutorService(@Nullable ExecutorService executorService) {if (TtlAgent.isTtlAgentLoaded() || executorService == null || executorService instanceof TtlEnhanced) {return executorService;}return new ExecutorServiceTtlWrapper(executorService, true);
}

包装了ExecutorService,执行结果也是符合预期

使用java Agent无侵入增强线程池

直接看代码

package com.tml.mouseDemo.core.threadLocalDemo;import com.alibaba.ttl.TransmittableThreadLocal;import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;public class ThreadLocalDemo1 {private static ThreadLocal<String> threadLocal = new TransmittableThreadLocal<>();private static ExecutorService service = Executors.newFixedThreadPool(2, new SimpleThreadFactory());public static void main(String[] args) throws InterruptedException {threadLocal.set("hello main");for (int i = 0; i < 2; i++) {service.execute(() -> {String s = threadLocal.get();ThreadUtils.printLog("get data " + s);});}//修改threadLocal中的值threadLocal.set("hello world");Thread.sleep(2000);for (int i = 0; i < 2; i++) {service.execute(() -> {String s = threadLocal.get();ThreadUtils.printLog("get data " + s);});}ThreadUtils.printLog("get data " + threadLocal.get());service.shutdown();}
}

 项目运行的时候,需要添加额外的jvm启动参数,如下

运行结果如下,也是符合预期

 2025-01-10 20:11:59 | INFO  | tml-2 | com.tml.mouseDemo.core.threadLocalDemo.ThreadUtils | get data hello main
2025-01-10 20:11:59 | INFO  | tml-1 | com.tml.mouseDemo.core.threadLocalDemo.ThreadUtils | get data hello main
2025-01-10 20:12:01 | INFO  | tml-2 | com.tml.mouseDemo.core.threadLocalDemo.ThreadUtils | get data hello world
2025-01-10 20:12:01 | INFO  | main | com.tml.mouseDemo.core.threadLocalDemo.ThreadUtils | get data hello world
2025-01-10 20:12:01 | INFO  | tml-1 | com.tml.mouseDemo.core.threadLocalDemo.ThreadUtils | get data hello world

总结

阿里巴巴的TransmittableThreadLocal是继承自InheritableThreadLocal,对他的功能进行了增强,增强的点也主要是在线程池的支持上。

通过上面的三个案例,可以看到TransmittableThreadLocal是非常灵活的,大家可以根据自己的需要,选择对应的方式来实现。

TransmittableThreadLocal的官网GitCode - 全球开发者的开源社区,开源代码托管平台

相关文章:

阿里巴巴TransmittableThreadLocal使用指南

前言 ThreadLocal在上下文的数据传输上非常的方便和简洁。工业实践中&#xff0c;比较常用的有三个&#xff0c;ThreadLocal、InheritableThreadLocal、TransmittableThreadLocal&#xff0c;那么他们三个之间有什么区别呢&#xff1f; 常见的三种ThreadLocal比较 ThreadLoc…...

ubuntu20下编译linux1.0 (part1)

author: hjjdebug date: 2025年 01月 09日 星期四 15:56:15 CST description: ubuntu20下编译linux1.0 (part1) 该博客记录了新gcc编译旧代码可能碰到的问题和解决办法, 可留作参考 操作环境: ubuntu20 $ gcc --version gcc (Ubuntu 9.4.0-1ubuntu1~20.04.2) 9.4.0 $ as --vers…...

欧拉公式和傅里叶变换

注&#xff1a;英文引文机翻&#xff0c;未校。 中文引文未整理去重&#xff0c;如有异常&#xff0c;请看原文。 Euler’s Formula and Fourier Transform Posted byczxttkl October 7, 2018 Euler’s formula states that e i x cos ⁡ x i sin ⁡ x e^{ix} \cos{x} i …...

Jenkins内修改allure报告名称

背景&#xff1a; 最近使用Jenkins搭建自动化测试环境时&#xff0c;使用Jenkins的allure插件生成的报告&#xff0c;一直显示默认ALLURE REPORT&#xff0c;想自定义成与项目关联的名称&#xff0c;如图所示&#xff0c;很明显自定义名称显得高大上些&#xff0c;之前…...

30天开发操作系统 第 12 天 -- 定时器 v1.0

前言 定时器(Timer)对于操作系统非常重要。它在原理上却很简单&#xff0c;只是每隔一段时间(比如0.01秒)就发送一个中断信号给CPU。幸亏有了定时器&#xff0c;CPU才不用辛苦地去计量时间。……如果没有定时器会怎么样呢?让我们想象一下吧。 假如CPU看不到定时器而仍想计量时…...

Ubuntu | PostgreSQL | 解决 ERROR: `xmllint` is missing on your system.

解决 sudo apt install apt-file sudo apt-file updatesudo apt-file search xmllint sudo apt install libxml2-utils执行 # postgres源码安装包解压文件夹中 make install make install问题 make -C src install make[2]: Entering directory /home/postgres/postgresql-1…...

uniapp使用chooseLocation安卓篇

本文章全部以高德地图为例 代码 <view class"bottom"><button click"choose">定位</button> </view> choose() {uni.chooseLocation({success: function(res) {console.log(位置名称&#xff1a; res.name);console.log(详细地…...

《PC 上的开源神经网络多模态模型:开启智能交互新时代》

《PC 上的开源神经网络多模态模型&#xff1a;开启智能交互新时代》 一、引言二、多模态模型基础剖析&#xff08;一&#xff09;核心概念解读&#xff08;二&#xff09;技术架构探秘 三、开源多模态模型的独特魅力&#xff08;一&#xff09;开源优势尽显&#xff08;二&…...

Apache JMeter 压力测试使用说明

文章目录 一、 安装步骤步骤一 下载相关的包步骤二 安装 Jmeter步骤三 设置 Jmeter 工具语言类型为中文 二、使用工具2.1 创建测试任务步骤一 创建线程组步骤二 创建 HTTP 请求 2.2 配置 HTTP 默认参数添加 HTTP消息头管理器HTTP请求默认值 2.3 添加 查看结果监听器2.4 查看结果…...

腾讯云AI代码助手编程挑战赛-知识百科AI

作品简介 知识百科AI这一编程主要用于对于小朋友的探索力的开发&#xff0c;让小朋友在一开始就对学习具有探索精神。在信息化时代下&#xff0c;会主动去学习自己认知以外的知识&#xff0c;同时丰富了眼界&#xff0c;开拓了新的知识。同时催生了在大数据时代下的信息共享化…...

【SpringAOP】Spring AOP 底层逻辑:切点表达式与原理简明阐述

前言 &#x1f31f;&#x1f31f;本期讲解关于spring aop的切面表达式和自身实现原理介绍~~~ &#x1f308;感兴趣的小伙伴看一看小编主页&#xff1a;GGBondlctrl-CSDN博客 &#x1f525; 你的点赞就是小编不断更新的最大动力 &am…...

HTTP-响应协议

HTTP的响应过程&#xff1f; 浏览器请求数据--》web服务器过程&#xff1a;请求过程 web服务器将响应数据-》到浏览器&#xff1a;响应过程 响应数据有哪些内容&#xff1f; 1.和请求数据类似。 2. 响应体中存储着web服务器返回给浏览器的响应数据。并且注意响应头和响应体之间…...

SQL进阶实战技巧:即时订单比例问题

目录 0 需求描述 1 数据准备 2 问题分析 3 小结 往期精彩 0 需求描述 订单配送中,如果期望配送日期和下单日期相同,称为即时订单,如果期望配送日期和下单日期不同,称为计划订单。 请从配送信息表(delivery_info)中求出每个用户的首单(用户的第一个订单)中即时订单…...

什么是端口

端口是用来区分同一网络设备(IP地址)上运行的不同服务或应用程序接收外部数据的窗口。 以下是几个要点&#xff1a; 对于我们发送请求指定的url中的端口&#xff0c;指的是对方服务器的用于接收数据的端口&#xff0c;如http的80端口&#xff0c;服务器通常都会设定要监听来自…...

【Flutter】使用ScrollController配合EasyRefresh实现列表预加载:在还未滑动到底部时加载下一页数据

需求/背景 在我们的业务场景中&#xff0c;列表的加载使用easy_refresh组件&#xff1a; https://pub.dev/packages/easy_refresh 大概效果是往上滑动到一定的offset会触发一个上滑加载&#xff0c;可以触发一些网络请求拉取列表后面的数据来展示。 这种模式一般在一页翻完…...

【2025 Rust学习 --- 11 实用工具特型01】

清理特型Drop 当一个值的拥有者消失时&#xff0c;Rust 会丢弃&#xff08;drop&#xff09;该值。丢弃一个值就必须释放 该值拥有的任何其他值、堆存储和系统资源。 丢弃可能发生在多种情况下&#xff1a; 当变量超出作用域时&#xff1b;在表达式语句的末尾&#xff1b;当…...

网络安全基础以及概念

1. 安全领域的概念 1.1 网络产品 1. EDR:终端检测与响应(Endpoint Detection and Response),终端主要包括我们的笔记本、台式机、手机、服务器等,EDR是一种运行在终端上安全软件,主要负责监控网络流量、可疑进程、注册表活动等其他安全相关的事件与活动。当发现有威胁是自…...

windows和linux的抓包方式

1.实验准备&#xff1a; 一台windows主机&#xff0c;一台linux主机 wireshark使用&#xff1a; 打开wireshark&#xff0c;这些有波动的就代表可以有流量经过该网卡&#xff0c;选择一张有流量经过的网卡 可以看到很多的流量&#xff0c;然后可以使用过滤器来过滤想要的流量…...

【Uniapp-Vue3】v-if条件渲染及v-show的选择对比

如果我们想让元素根据响应式变量的值进行显示或隐藏可以使用v-if或v-show 一、v-show 另一种控制显示的方法就是使用v-show&#xff0c;使用方法和v-if一样&#xff0c;为true显示&#xff0c;为false则不显示。 二、v-if v-if除了可以像v-show一样单独使用外&#xff0c;还…...

宝塔面板使用 GoAccess Web 日志分析教程

宝塔面板是一个简单方便的服务器运维面板,但其网站统计功能是收费的。而 GoAccess 是一个用 C 编写的免费开源 Web日志分析器,本文将介绍如何在宝塔面板中开启 GoAccess Web 日志分析功能。 内容索引 下载安装 GoAccess在宝塔面板中添加日志切割的计划任务将 Web 日志输出到…...

springboot 百货中心供应链管理系统小程序

一、前言 随着我国经济迅速发展&#xff0c;人们对手机的需求越来越大&#xff0c;各种手机软件也都在被广泛应用&#xff0c;但是对于手机进行数据信息管理&#xff0c;对于手机的各种软件也是备受用户的喜爱&#xff0c;百货中心供应链管理系统被用户普遍使用&#xff0c;为方…...

DeepSeek 赋能智慧能源:微电网优化调度的智能革新路径

目录 一、智慧能源微电网优化调度概述1.1 智慧能源微电网概念1.2 优化调度的重要性1.3 目前面临的挑战 二、DeepSeek 技术探秘2.1 DeepSeek 技术原理2.2 DeepSeek 独特优势2.3 DeepSeek 在 AI 领域地位 三、DeepSeek 在微电网优化调度中的应用剖析3.1 数据处理与分析3.2 预测与…...

Python:操作 Excel 折叠

💖亲爱的技术爱好者们,热烈欢迎来到 Kant2048 的博客!我是 Thomas Kant,很开心能在CSDN上与你们相遇~💖 本博客的精华专栏: 【自动化测试】 【测试经验】 【人工智能】 【Python】 Python 操作 Excel 系列 读取单元格数据按行写入设置行高和列宽自动调整行高和列宽水平…...

css的定位(position)详解:相对定位 绝对定位 固定定位

在 CSS 中&#xff0c;元素的定位通过 position 属性控制&#xff0c;共有 5 种定位模式&#xff1a;static&#xff08;静态定位&#xff09;、relative&#xff08;相对定位&#xff09;、absolute&#xff08;绝对定位&#xff09;、fixed&#xff08;固定定位&#xff09;和…...

前端开发面试题总结-JavaScript篇(一)

文章目录 JavaScript高频问答一、作用域与闭包1.什么是闭包&#xff08;Closure&#xff09;&#xff1f;闭包有什么应用场景和潜在问题&#xff1f;2.解释 JavaScript 的作用域链&#xff08;Scope Chain&#xff09; 二、原型与继承3.原型链是什么&#xff1f;如何实现继承&a…...

rnn判断string中第一次出现a的下标

# coding:utf8 import torch import torch.nn as nn import numpy as np import random import json""" 基于pytorch的网络编写 实现一个RNN网络完成多分类任务 判断字符 a 第一次出现在字符串中的位置 """class TorchModel(nn.Module):def __in…...

Java求职者面试指南:Spring、Spring Boot、MyBatis框架与计算机基础问题解析

Java求职者面试指南&#xff1a;Spring、Spring Boot、MyBatis框架与计算机基础问题解析 一、第一轮提问&#xff08;基础概念问题&#xff09; 1. 请解释Spring框架的核心容器是什么&#xff1f;它在Spring中起到什么作用&#xff1f; Spring框架的核心容器是IoC容器&#…...

Go 并发编程基础:通道(Channel)的使用

在 Go 中&#xff0c;Channel 是 Goroutine 之间通信的核心机制。它提供了一个线程安全的通信方式&#xff0c;用于在多个 Goroutine 之间传递数据&#xff0c;从而实现高效的并发编程。 本章将介绍 Channel 的基本概念、用法、缓冲、关闭机制以及 select 的使用。 一、Channel…...

libfmt: 现代C++的格式化工具库介绍与酷炫功能

libfmt: 现代C的格式化工具库介绍与酷炫功能 libfmt 是一个开源的C格式化库&#xff0c;提供了高效、安全的文本格式化功能&#xff0c;是C20中引入的std::format的基础实现。它比传统的printf和iostream更安全、更灵活、性能更好。 基本介绍 主要特点 类型安全&#xff1a…...

小木的算法日记-多叉树的递归/层序遍历

&#x1f332; 从二叉树到森林&#xff1a;一文彻底搞懂多叉树遍历的艺术 &#x1f680; 引言 你好&#xff0c;未来的算法大神&#xff01; 在数据结构的世界里&#xff0c;“树”无疑是最核心、最迷人的概念之一。我们中的大多数人都是从 二叉树 开始入门的&#xff0c;它…...