alibaba / transmittable-thread-local

📌 a missing Java std lib(simple & 0-dependency) for framework/middleware, provide an enhanced InheritableThreadLocal that transmits values between threads even using thread pooling components.
https://github.com/alibaba/transmittable-thread-local
Apache License 2.0
7.65k stars 1.69k forks source link

TtlRunnable A run执行最后一行sleep, TtlRunnable B执行完毕 释放TransmittableThread Local holder, TtlRunnable A进入restore holder还是有值怎么做到的 #142

Closed qq31715879 closed 5 years ago

qq31715879 commented 5 years ago

源码学习提问

Runnable runnable = new Runnable() {
    @Override
    public void run() {
        System.out.println(transmittableThreadLocal.get());
        transmittableThreadLocal.set("aaaa");
        System.out.println(transmittableThreadLocal.get());
        transmittableThreadLocal.set("bbb");
        System.out.println(transmittableThreadLocal.get());

        try {
            Thread.sleep(7000);
        } catch (InterruptedException e) {
        }
        System.out.println("Thread 1 执行完毕");
    }
};

ExecutorService executorService = Executors.newFixedThreadPool(2);
TtlRunnable ttlRunnable = TtlRunnable.get(runnable);
executorService.execute(ttlRunnable);

Runnable runnable1 = new Runnable() {
    @Override
    public void run() {
        System.out.println(transmittableThreadLocal.get());
        transmittableThreadLocal.set("111");
        System.out.println(transmittableThreadLocal.get());
        transmittableThreadLocal.set("222");
        System.out.println(transmittableThreadLocal.get());
        System.out.println("Thread 2 执行完毕");
    }
};
TtlRunnable ttlRunnable1 = TtlRunnable.get(runnable1);
executorService.submit(ttlRunnable1);


start restore TransmittableThreadLocal$1@4449983f {TransmittableThreadLocal@5754de5a=null}
    for holder.get() size 1
remove 222
end restore TransmittableThreadLocal$1@4449983f {}  holder.get() size 0
end restore TransmittableThreadLocal$1@4449983f {}  holder.get() size 0
setTtlValuesTo restore TransmittableThreadLocal$1@4449983f {}  holder.get() size 0
restore 结束执行
restore 结束执行 TransmittableThreadLocal$1@4449983f {}  holder.get() size 0
Thread 1 执行完毕
run 执行完毕
run 执行完毕 TransmittableThreadLocal$1@4449983f {TransmittableThreadLocal@5754de5a=null}
    holder.get() size 1

# 这里ttlrunable A sleep完了之后开始restore
# 但是TransmittableThreadLocal.holder突然又有值了
# 这里的值啥时候又塞进去了

restore 开始执行
restore 开始执行 TransmittableThreadLocal$1@4449983f {TransmittableThreadLocal@5754de5a=null}
    holder.get() size 1
restore backupMap 0
doExecuteCallback bbb
start restore TransmittableThreadLocal$1@4449983f {TransmittableThreadLocal@5754de5a=null}
    for holder.get() size 1
remove bbb
end restore TransmittableThreadLocal$1@4449983f {} holder.get() size 0
end restore TransmittableThreadLocal$1@4449983f {} holder.get() size 0
setTtlValuesTo restore TransmittableThreadLocal$1@4449983f {}  holder.get() size 0
restore 结束执行
restore 结束执行 TransmittableThreadLocal$1@4449983f {}  holder.get() size 0
oldratlee commented 5 years ago
  • TtlRunnable A和B共同使用一个TransmittableThreadLocal
  • TtlRunnable A run执行最后一行sleep
  • TtlRunnable B run执行完毕 -> 释放TransmittableThreadLocal holder,holder中的map size == 0
  • TtlRunnable A开始进入restore,但是进入方法之前,holder中的map值被恢复了,holder还是有值 怎么做到的?

你的信息说明 还不清楚。比如

『TtlRunnable A开始进入restore,但是进入方法之前,holder中的map值被恢复了』

不清楚你说的是,恢复 成什么了?你期望的恢复 成什么?


@qq31715879 推荐:

qq31715879 commented 5 years ago

感谢提醒, 想起来了 holder本身是一个Threadlocal所以线程b释放的是自己的threadlocal 所以有值

qq31715879 commented 5 years ago

感谢大佬回复

------------------ 原始邮件 ------------------ 发件人: "李鼎(哲良)"notifications@github.com; 发送时间: 2019年7月10日(星期三) 晚上8:38 收件人: "alibaba/transmittable-thread-local"transmittable-thread-local@noreply.github.com; 抄送: "kael"31715879@qq.com;"Mention"mention@noreply.github.com; 主题: Re: [alibaba/transmittable-thread-local] TtlRunnable A run执行最后一行sleep, TtlRunnable B执行完毕 释放TransmittableThread Local holder, TtlRunnable A进入restore holder还是有值怎么做到的 (#142)

TtlRunnable A和B共同使用一个TransmittableThreadLocal

TtlRunnable A run执行最后一行sleep

TtlRunnable B run执行完毕 -> 释放TransmittableThreadLocal holder,holder中的map size == 0

TtlRunnable A开始进入restore,但是进入方法之前,holder中的map值被恢复了,holder还是有值 怎么做到的?

你的信息说明 还不清楚。比如

『TtlRunnable A开始进入restore,但是进入方法之前,holder中的map值被恢复了』

不清楚你说的是,恢复 成什么了?你期望的恢复 成什么?

@qq31715879 推荐:

要注意的是holder本身是一个InheritableThreadLocal。

文档:所有TTL值的抓取、回放和恢复方法(即CRR操作)

可以看看『小伙伴同学们写的 设计实现解析文章(写得都很好! )❤️ #123』
推荐 梳理一下TransmittableThreadLocal的系统流程、查看代码实现。

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or mute the thread.