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

请问下,在spring boot 场景下,filter 中进入时set ,但是业务中异步线程还未执行完,filter 请求已经finally remove了,这时获取到的值为null, 这种情况如何保证正确性 #641

Closed
freshgeek opened this issue May 6, 2024 · 3 comments
Assignees
Labels
❓question Further information is requested

Comments

@freshgeek
Copy link

请问下,在spring boot 场景下,filter 中进入时set ,但是业务中异步线程还未执行完,filter 请求已经finally remove了,这时获取到的值为null, 这种情况如何保证正确性

@oldratlee
Copy link
Member

oldratlee commented May 6, 2024

对于TTL本身逻辑/功能,
主线程的修改(filter请求的remove)不会影响 给子任务传递了的值。

一般是因为ThreadLocal的值使用了共享的集合(如Map),所以修改子任务可见。可以看看: @freshgeek

这个TTL使用方式/问题的解决方法,可以看看:

更多可以看看:

@oldratlee oldratlee added the ❓question Further information is requested label May 6, 2024
@freshgeek
Copy link
Author

freshgeek commented May 6, 2024

TransmittableThreadLocal数据丢失问题
https://juejin.cn/post/7311500203432575026

@oldratlee
Copy link
Member

oldratlee commented May 6, 2024

TransmittableThreadLocal数据丢失问题 https://juejin.cn/post/7311500203432575026

executor.setTaskDecorator(runnable -> TtlRunnable.get(runnable, true));

// 推荐改成

executor.setTaskDecorator(TtlRunnable::get);

// 以避免不能在线程池中运行(可能)多次重复运行的任务

@oldratlee oldratlee self-assigned this May 18, 2024
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

2 participants