Closed
Description
The core scenario is transmit value value from the time task is created to the time task is executed, so transmitting value new Thread
(Inheritable) maybe is not necessary and buggy(eg. has potential memory/context leaking problem).
But when use thread pool, thread is cached up and used repeatedly. Transmitting value from parent thread to child thread has no meaning. Application need transmit value from the time task is created to the time task is executed.
Metadata
Metadata
Assignees
Type
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
driventokill commentedon Sep 21, 2018
Transmit thread local values by inheritable thread local is a potential leaking problem. A new created thread can never know how to deal with the inherited values. The transmittable thread local values should be created and cleared within a task's lifecycle. InheritableThreadLocal will leak the management of transmittable thread local values out of a task.
+ support disable inheritable feature
+ support optional inheritable feature
driventokill commentedon Oct 12, 2018
Here are some thoughts about the inheritable feature:
case 1
Inheritable feature in thread pooling components(
ThreadPoolExecutor
etc.) should never happen, because threads in thread pooling components is pre-created and pooled, so these threads is neutral for biz logic/data.case 2
Whether the value should be inheritable or not can be controlled by the data owner, disable it carefully when data owner have a clear idea.
disable
inheritable
by overriding thechildValue
and returninitialValue()
:disable ttl inheritable for thread created by thread pool #100
+ add disable inheritable usage and unit test for TransmittableThread…
[-]add option for TTL, disable Inheritable[/-][+]disable Inheritable when it's not necessary and buggy(eg. has potential memory leaking problem)[/+]driventokill commentedon Nov 6, 2018
Solved.
youzipi commentedon Jan 9, 2019
为什么不直接 继承 ThreadLocal 呢?
我理解的 TTL 和 ITL 是解决不同场景的方案,从出发点就不一样。
或者可以在 构造器 增加参数-
inheritable
,默认为 falseoldratlee commentedon Jan 9, 2019
@youzipi
在业务中可以直接以
new Thread
这样的方式 来 使用,正确完成传递 是 期望的功能。# 虽然,有了线程池,直接
new
线程的做法,极有可能是不应该的(不是最佳实践!)。选择继承
ITL
而不直接继承ThreadLocal
,原因如下:new Thread
的情况下,也能进行传递。即包含上面new Thread
的传递功能。ThreadLocal
,对于使用了ITL
的业务代码,则不方便切成TTL
了。而
inheritable
在业务上 有潜在的(potential)的 泄漏或污染。这样的情况下,期望进一步方便地解决好~
要说明/解决问题,你需要给出 全场景 下的说明,比如说明出:
当
TTL
/ITL
在继承上下文有泄漏或污染问题时,应该全部使用成ThreadLocal
且 对应的业务功能 也都能解决好。那么,就可以推出你的结论:『
TTL
和ITL
是解决不同场景的方案,从出发点就不一样』。