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

可不可对原生的ThreadLocal起作用? #329

Closed
kaneg opened this issue Nov 28, 2021 · 6 comments
Closed

可不可对原生的ThreadLocal起作用? #329

kaneg opened this issue Nov 28, 2021 · 6 comments
Assignees
Labels
❓question Further information is requested

Comments

@kaneg
Copy link

kaneg commented Nov 28, 2021

你好:

我有一个项目,我能控制线程池和Runnable的创建,但我没法让其他人把ThreadLocal替换为TransmittableThreadLocal,请问有没有办法可以用你们的方案来达到效果?谢谢。

@oldratlee
Copy link
Member

oldratlee commented Nov 28, 2021

可不可对原生的ThreadLocal起作用?

可以。

TTL提供了Transmitter.registerThreadLocal*方法用于让ThreadLocal支持Transmittable能力,@kaneg 了解一下。

更多使用说明与注意 参见 Transmitter JavaDoc

image

相关Issue:


欢迎反馈使用中的问题与建议~ ❤️

@oldratlee oldratlee self-assigned this Nov 28, 2021
@oldratlee oldratlee added the ❓question Further information is requested label Nov 28, 2021
@kaneg
Copy link
Author

kaneg commented Nov 28, 2021

@oldratlee 谢谢你的快速回应。
用Transmitter的register的确可以达到我期望的效果。不过有一点缺憾,就是thread local变量被别人封装为private了,目前在没有更好的方案之前,我用反射也是可以拿到的。

@oldratlee
Copy link
Member

oldratlee commented Nov 28, 2021

用Transmitter的register的确可以达到我期望的效果。不过有一点缺憾,就是thread local变量被别人封装为private了,目前在没有更好的方案之前,我用反射也是可以拿到的。

『thread local变量封装为private』(这往往是合理的做法),所以自然需用hack的方式获取。

反射应该是最简单直接的获取方式了(三五行代码) 😄 @kaneg

@oldratlee
Copy link
Member

oldratlee commented Nov 28, 2021

@kaneg 可以介绍一下你项目中实际的场景、需求、问题吗? ❤️
方便大家可以了解学习。

@kaneg
Copy link
Author

kaneg commented Dec 2, 2021

@oldratlee 我们的项目是一个基于Spring Boot的微服务。我们使用了其他部门的一些依赖库,这些依赖库中大量使用了ThreadLocal来传递参数,例如用户的id,http 的请求参数等等。但是我们在自己的代码中用了CompletableFuture来异步调用后台的API, 就导致在子线程中没法拿到用户id等信息. 你们这个库恰恰就解决了我们的痛点.

oldratlee added a commit that referenced this issue Jul 1, 2022
… getting `ThreadLocal` instances by reflection #329
oldratlee added a commit that referenced this issue Jul 1, 2022
… getting `ThreadLocal` instances by reflection #329
@oldratlee
Copy link
Member

oldratlee commented Jul 1, 2022

『thread local变量封装为private』(这往往是合理的做法),所以自然需用hack的方式获取。

反射应该是最简单直接的获取方式了(三五行代码) 😄 @kaneg

@kanegregisterThreadLocal*的 JavaDoc中,
加了 通过反射来获取private static ThreadLocal实例的简单说明:

The fields stored the ThreadLocal instances are generally private static, so the ThreadLocal instances need be got by reflection, for example:

Field field = TheClassStoredThreadLocal.class.getDeclaredField(staticFieldName);
field.setAccessible(true);
@SuppressWarnings("unchecked")
ThreadLocal<T> threadLocal = (ThreadLocal<T>) field.get(null);

提交: 9d40c2e

oldratlee added a commit that referenced this issue Jul 4, 2022
… getting `ThreadLocal` instances by reflection #329
oldratlee added a commit that referenced this issue Jul 4, 2022
… getting `ThreadLocal` instances by reflection #329
oldratlee added a commit that referenced this issue Jul 4, 2022
… getting `ThreadLocal` instances by reflection #329
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