Giter Club home page Giter Club logo

Comments (6)

mingyang66 avatar mingyang66 commented on June 18, 2024 2

我在#521这个ISSUE中找到了想要的答案, @oldratlee 感谢

from transmittable-thread-local.

mingyang66 avatar mingyang66 commented on June 18, 2024 1

非常感谢 @oldratlee 的回答,今天花了一天时间研究这一块,其实值传递的核心是

  • capture【获取父线程中的值,包括引用对象或普通对象】
  • replay【回放:备份、将父线程的值设置到子线程】
  • restore【会将子线程执行之前backup的值设置回子线程的ThreadLocal】;

如果通过如下关闭Interitable能力:

          @Override
          protected User childValue(User parentValue) {
              return initialValue();
          }

那么在子线程replay回放时backup就不存在,所有在restore恢复的时候子线程其实是不存在值恢复的;

如果不关闭Interitable能力,那么子线程继承了父线程的值【普通对象、引用类型】,在restore后会将子线程的值恢复成从父线程继承的值或引用类型,但是如果子线程执行结束了,那么子线程的ThreadLocal就不会持有父线程的上下文了,这是因为ThreadLocal的生命周期是和线程的生命周期相关联,线程结束,则ThreadLocal变量也会自动被GC销毁;

还有个问题,就是线程池复用的问题,如果不关闭线程的Interitable能力,则子线程继承父线程的引用对象,如果执行完成后被线程池复用,那么就会有一部分引用对象一直无法释放,如果这个量比较大的话有可能造成OOM,像这种问题又是如何解决的呢?

有空了帮忙解答下,谢谢!

from transmittable-thread-local.

mingyang66 avatar mingyang66 commented on June 18, 2024

TransmittableThreadLocal内部本身定义了holder变量如下:

    private static final InheritableThreadLocal<WeakHashMap<TransmittableThreadLocal<Object>, ?>> holder =
            new InheritableThreadLocal<WeakHashMap<TransmittableThreadLocal<Object>, ?>>() {
                @Override
                protected WeakHashMap<TransmittableThreadLocal<Object>, ?> initialValue() {
                    return new WeakHashMap<>();
                }

                @Override
                protected WeakHashMap<TransmittableThreadLocal<Object>, ?> childValue(WeakHashMap<TransmittableThreadLocal<Object>, ?> parentValue) {
                    return new WeakHashMap<TransmittableThreadLocal<Object>, Object>(parentValue);
                }
            };

其中InheritableThreadLocal内部存储的是一个WeakHashMap类型,其键是一个WeakReference类型,如果没有强引用的话就会在下次GC的时候回收掉,而其value本身就是null,所以对于TTL本身来说是不存在内存溢出问题的;

对于子线程而言其值是存在于Thread线程的ThreadLocal.ThreadLocalMap inheritableThreadLocals变量中,而ThreadLocalMap内部的存储结构是一个WeakReference类型,如果没有强引用则会在下次GC的时候置为null,在下次使用的时候会主动清除掉key为null引用,从而避免OOM;

上述异步线程池中最外层主线程主动remove,内部无法主动remove,而不会造成OOM的原因是不是上述分析的 @oldratlee 帮忙解答下,谢谢

from transmittable-thread-local.

oldratlee avatar oldratlee commented on June 18, 2024

因为有「恢复(Transmitter.restore())」操作做了清除操作,
在运行完任务之后子线程(的ThreadLocal)不再持有上下文,
所以不会因为这个传递过程而引入内存泄露。 @mingyang66
注意:上面的过程 不包含「由Inheritable能力(InheritableThreadLocal) 带给子线程的上下文」这个情况。

对于Inheritable能力引起的「内存泄露」,有较多讨论,
可以看看网上的讨论、或这个库涉及「内存泄露」相关的issue。

TTL提供了关闭Inheritable的一些方法,具体参见TransmitableThreadLocal的JavaDoc。

from transmittable-thread-local.

mingyang66 avatar mingyang66 commented on June 18, 2024

我看很多案例和issue中都没有调用TransmittableThreadLocal.remove方法移除上下文的操作,是不是因为TTL中使用WeakHashMap和ThreadLocal使用WeakRefrence的原因?GC的时候会自动回收?

from transmittable-thread-local.

oldratlee avatar oldratlee commented on June 18, 2024

我看很多案例和issue中都没有调用TransmittableThreadLocal.remove方法移除上下文的操作,是不是因为TTL中使用WeakHashMap和ThreadLocal使用WeakRefrence的原因?GC的时候会自动回收?

@mingyang66 上一条评论有原因的一些解释:

因为有「恢复(Transmitter.restore())」操作做了清除操作,
在运行完任务之后子线程(的ThreadLocal)不再持有上下文,
所以不会因为这个传递过程而引入内存泄露。 @mingyang66
注意:上面的过程 不包含「由Inheritable能力(InheritableThreadLocal) 带给子线程的上下文」这个情况。

如果想更多深入理解说明TTL的实现设计,

  • 可能直接看源码研究实现逻辑、并运行调试验证 是一个比较有效的方法。 @mingyang66
    • (我的语言解释起来 并不简单直观 😬
  • 当然TTL有不少介绍与解析的文章(解释得比我好 💕):

from transmittable-thread-local.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.