Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
qq31715879 opened this issue Jul 9, 2019 · 3 comments
Assignees
Labels
❓question Further information is requested

Comments

@qq31715879
Copy link

qq31715879 commented Jul 9, 2019

源码学习提问

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);

  • TtlRunnable A和B共同使用一个TransmittableThreadLocal
  • TtlRunnable A run执行最后一行sleep
  • TtlRunnable B run执行完毕 -> 释放TransmittableThreadLocal holder,holder中的map size == 0
  • TtlRunnable A开始进入restore,但是进入方法之前,holder中的map值被恢复了,holder还是有值
    怎么做到的?

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
@driventokill driventokill added 🐞 bug Something isn't working ❓question Further information is requested and removed 🐞 bug Something isn't working labels Jul 10, 2019
@oldratlee
Copy link
Member

oldratlee commented Jul 10, 2019

  • 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
Copy link
Author

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

@qq31715879
Copy link
Author

qq31715879 commented Jul 11, 2019 via email

@oldratlee oldratlee self-assigned this Jul 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
❓question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants