ThreadLocal - 如果我们在构造函数中设置值,是否需要它?
我有一个类 MyThread 实现 Callable
在此过程中,我从 hashmap 中获取这个阻塞队列,添加我的自定义消息并获取相应的 MyThread 实例并提交给 ThreadPoolTaskExecutor (Spring 版本)。
据我了解,每个线程都应该获取自己的值副本(例如阻塞队列等),就像在构造过程中传递它们并稍后使用 ThreadPoolTaskExecutor.submit(myThreadObj) 创建线程一样。我只是想知道是否有人可以确认这是否属实或者我是否需要在这种情况下使用 ThreadLocal。到目前为止的测试,我没有遇到问题,但我还没有进行负载测试。
提前致谢。
I have a class, MyThread which implements Callable <String>. The class has a constructor which takes parameters like a blockingqueue and others. In my main class, I instantiate this class, MyThread, with a new blocking queue and other parameters. Also, I maintain two maps, one to keep MyThread reference with one of unique parameter as key and other to keep blocking queue reference with the same unique key.
During the process, I get this blocking queue from the hashmap, add my custom message and take the corresponding MyThread instance and submit to ThreadPoolTaskExecutor (Spring version).
As far as I understand, each thread should get its own copy of values (like blocking queue etc), as am passing them during the construction and creating a thread later using ThreadPoolTaskExecutor.submit(myThreadObj). I was just wondering if someone could confirm if that is true OR whether I am required to use ThreadLocal in this scenario. So far with the testing, I did not encounter problem, but then I am yet to do the load testing.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您有效地重新发明了 ThreadLocal。享受!
You have effectively reinvented ThreadLocal. Enjoy!