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

TransmittableThreadLocal的传递只有浅拷贝吗? #111

Closed
Ronustine opened this issue Sep 7, 2018 · 2 comments
Closed

TransmittableThreadLocal的传递只有浅拷贝吗? #111

Ronustine opened this issue Sep 7, 2018 · 2 comments
Assignees
Labels
🔰 first nice issue 👍 ❓question Further information is requested

Comments

@Ronustine
Copy link

Ronustine commented Sep 7, 2018

Hello,做项目是遇到一个问题:

用线程池去执行一个执行时间较长的任务,但是任务线程需要父线程的ThreadLocal。而TransmittableThreadLocal传递似乎只是传递了引用,并不是深拷贝。

这样会引起一个问题,父线程ThreadLocal如果置空或者值有修改的情况下,任务线程也会随之改变。我觉得在任务提交后,值不应该有变动吧?

请问能否控制拷贝方式呢?

不好意思,第一次提问,请多包涵哈,若能帮忙解惑,不胜感激~

@oldratlee
Copy link
Member

oldratlee commented Sep 7, 2018

值传递拷贝方式通过覆写TransmittableThreadLocalprotected copy方法来定制,缺省是直接传的是引用。

你可以实现成『浅拷贝』、『深拷贝』或是面向你需求的拷贝方式。

你看看,这个 copy方法 是 你说的问题的解决方法 吗? 😁 @Ronustine


PS,澄清一下 你Issue中涉及的概念:

  • 『引用传递』概念 不同于 『浅拷贝』;
  • 浅拷贝 一般指:生成一个新的对象,但原来对象的字段只是传递引用。
    即只拷贝了一层。

PPS,能具体些说明一下你的使用场景吗?方便 记到 #73 ? ❤️ @Ronustine

@oldratlee oldratlee added the ❓question Further information is requested label Sep 7, 2018
@oldratlee oldratlee self-assigned this Sep 7, 2018
@SZMOFEI
Copy link

SZMOFEI commented Apr 30, 2022

niubi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🔰 first nice issue 👍 ❓question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants