Comments (3)
感谢大佬,上面的Case自己也整理清楚了,说明如下:
这里的最主要是识别TransmittableThreadLocal
继承自JDK
的InheritableThreadLocal
,参考上面的代码当t1.set("val1")
,执行后相当于主线程InheritableThreadLocal
这个线程绑定的值有值了。
之后第一次ThreadPoolExecutor
执行异步任务的时候,会初始化线程,初始化线程的特性会子线程也继承父线程;也就是主线程的InheritableThreadLocal
。
这个时候相当于父子线程都携带了InheritableThreadLocal
,后续主线程remove
操作,remove
的是主线程的InheritableThreadLocal
,finally
执行第二次异步任务的时候,线程池的线程是携带InheritableThreadLocal
,所以导致能够取到值。
即便使用TtlRunnable.get(runnable)
修饰第二个任务,能够符合预期获取空值,因为修饰后获取到的是主线程已经remove
的InheritableThreadLocal
;
但是会发生泄漏的问题,并没有remove
掉子线程的InheritableThreadLocal
,需要使用
TtlExecutors.getDefaultDisableInheritableThreadFactory()
- 或者
TransmittableThreadLocal<String> t1 = new TransmittableThreadLocal<String>() { protected String childValue(String parentValue) { return initialValue(); } }
声明解决泄漏问题。
第一种是业务操作前 清空线程池线程上下文;
第二种是父子线程继承的时候 子线程初始化为空值。
from transmittable-thread-local.
关于「不修饰」runnable使用ttl导致子线「泄露」
首先请以正确的方式使用TTL
。
参见项目文档 User Guide。
如果不做相应的修饰,TTL
会退化成InheritableThreadLocal
,
出现你说的情况是预期的(多提交几次可能非必现,与提交时间点/线程池配置相关);
更多请了解调研InheritableThreadLocal
与 ThreadPoolExecutor
。 @huhaosumail
其次请提供一个 极简的 可运行复现问题 的代码实现/Demo。推荐提供成一个单独的工程(Github repo),或是 一个运行出的问题的UT(可以Fork TTL加一个UT)。
这样可以
- 方便大家能排查分析(只提供运行结果,排查信息不足)
- 方便分离不相关的业务实现内容,以及排除可能的业务使用问题
一些相关说明
传递并不会消耗父线程的上下文。 @huhaosumail
要不只有一个子线程能拿到上下文了。
(这样的功能,在日常业务中,奇怪 应该也无用)
如果这导致业务的内存泄露,可以自己在finally
中remove
一下。
更多TTL
的了解资料可以看看:
另一个相关「泄露」的使用注意事项。 @huhaosumail
(JDK ThreadLocal
也有「内存泄露」的使用注意事项 💕)
请使用getDisableInheritableThreadFactory(...)
wrapper。
解决方法是
- 线程池线程不应该有无用的上下文,
- 或说 保证线程池线程刚开始时(所有业务逻辑的外层) 应该是 空的上下文,做好清空操作。
TTL
有讨论 & 提供了相应的功能实现: @OrientationZero
- 已有的讨论: disable Inheritable when it's not necessary and buggy(eg. has potential memory leaking problem) #100
- 对应
TTL
已实现的功能,即上面举例的DisableInheritableThreadFactoryWrapper
:
更多说明参见已有 issue
from transmittable-thread-local.
@huhaosumail COOOL
from transmittable-thread-local.
Related Issues (20)
- withInitialAndCopier method is incompatible with versions before jdk1.8 HOT 1
- skywalking集成ttl 采集多线程数据无效 HOT 1
- 复现TransmittableThreadLocal数据在「不用TtlExecutors包裹的线程池」中概率丢失。 HOT 1
- TTL是否适用于tomcat的StandardThreadExecutor,尝试了agent的方式和显式包装的方式都不行 HOT 1
- 线程池每次执行完成后默认将上下文还原至调用线程的上下文原状,可以执行完成后将线程中的上下文全部清除掉吗 HOT 1
- 集成`Spring Reactor` HOT 1
- jdk11 报错。 HOT 4
- ttl是否不支持rocketMQ的上下文传递 HOT 1
- 在CompletableFuture中使用TTL,主线程执行完成,但CompletableFuture未完成,是否可以可以释放上下文,主线程释放上下文后,CompletableFuture是否还能拿到TTL的值 HOT 1
- 加入agent后报TtlTransformer: Fail to transform class xxx 错误 HOT 4
- agent中对普通Thread场景进行包装,来触发copy方法 HOT 2
- agent方式使用TTL,jdk从8升到11之后项目启动报错 类重复加载: java.lang.LinkageError: loader java.net.URLClassLoader attempted duplicate class HOT 3
- “业务项目”在通过bytebuddy增强后,TTL+CompletableFuture无法再正常地进行上下文传递。 HOT 1
- -
- 与其他探针放在一起使用的时候,如果ttlagent顺序在其他探针之后,探针失效无法跨线程 HOT 7
- 关于registerThreadLocal方法加锁和copy on write疑问 HOT 1
- 有必要关闭Inheritable能力么 HOT 5
- TTL使用完成后需要remove掉当前线程的值吗?如果不删除会不会引发OOM? HOT 3
- 主线程执行完后会remove,但是子线程执行完成后会restore恢复本地变量,子线程中的值变量如何清除? HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from transmittable-thread-local.