Giter Club home page Giter Club logo

Comments (3)

huhaosumail avatar huhaosumail commented on June 27, 2024 3

感谢大佬,上面的Case自己也整理清楚了,说明如下:

这里的最主要是识别TransmittableThreadLocal继承自JDKInheritableThreadLocal,参考上面的代码当t1.set("val1"),执行后相当于主线程InheritableThreadLocal这个线程绑定的值有值了。

之后第一次ThreadPoolExecutor执行异步任务的时候,会初始化线程,初始化线程的特性会子线程也继承父线程;也就是主线程的InheritableThreadLocal

这个时候相当于父子线程都携带了InheritableThreadLocal,后续主线程remove操作,remove的是主线程的InheritableThreadLocalfinally执行第二次异步任务的时候,线程池的线程是携带InheritableThreadLocal,所以导致能够取到值。

即便使用TtlRunnable.get(runnable)修饰第二个任务,能够符合预期获取空值,因为修饰后获取到的是主线程已经removeInheritableThreadLocal

但是会发生泄漏的问题,并没有remove掉子线程的InheritableThreadLocal,需要使用

  • TtlExecutors.getDefaultDisableInheritableThreadFactory()
  • 或者
    TransmittableThreadLocal<String> t1 = new TransmittableThreadLocal<String>() {
        protected String childValue(String parentValue) {
            return initialValue();
        }
    }

声明解决泄漏问题。

第一种是业务操作前 清空线程池线程上下文;
第二种是父子线程继承的时候 子线程初始化为空值。

from transmittable-thread-local.

oldratlee avatar oldratlee commented on June 27, 2024

关于「不修饰」runnable使用ttl导致子线「泄露」

首先请以正确的方式使用TTL
参见项目文档 User Guide。

如果不做相应的修饰,TTL会退化成InheritableThreadLocal
出现你说的情况是预期的(多提交几次可能非必现,与提交时间点/线程池配置相关);
更多请了解调研InheritableThreadLocalThreadPoolExecutor@huhaosumail

其次请提供一个 极简的 可运行复现问题 的代码实现/Demo。推荐提供成一个单独的工程(Github repo),或是 一个运行出的问题的UT(可以Fork TTL加一个UT)。

这样可以

  • 方便大家能排查分析(只提供运行结果,排查信息不足)
  • 方便分离不相关的业务实现内容,以及排除可能的业务使用问题

一些相关说明

传递并不会消耗父线程的上下文。 @huhaosumail
要不只有一个子线程能拿到上下文了。
(这样的功能,在日常业务中,奇怪 应该也无用)

如果这导致业务的内存泄露,可以自己在finallyremove一下。

更多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
    * Wrapper of {@link ThreadFactory}, disable inheritable.
    *
    * @param threadFactory input thread factory
    * @see DisableInheritableThreadFactory
    * @see TtlForkJoinPoolHelper#getDisableInheritableForkJoinWorkerThreadFactory
    * @since 2.10.0
    */
    @Nullable
    @Contract(value = "null -> null; !null -> !null", pure = true)
    public static ThreadFactory getDisableInheritableThreadFactory(@Nullable ThreadFactory threadFactory) {
    if (threadFactory == null || isDisableInheritableThreadFactory(threadFactory)) return threadFactory;
    return new DisableInheritableThreadFactoryWrapper(threadFactory);
    }
    /**
    * Wrapper of {@link Executors#defaultThreadFactory()}, disable inheritable.
    *
    * @see #getDisableInheritableThreadFactory(ThreadFactory)
    * @see TtlForkJoinPoolHelper#getDefaultDisableInheritableForkJoinWorkerThreadFactory()
    * @since 2.10.0
    */
    @NonNull
    public static ThreadFactory getDefaultDisableInheritableThreadFactory() {
    return getDisableInheritableThreadFactory(Executors.defaultThreadFactory());

更多说明参见已有 issue

from transmittable-thread-local.

oldratlee avatar oldratlee commented on June 27, 2024

@huhaosumail COOOL 👍 🚀

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.