开发者

ThreadLocal - Is it required if we are setting the value in constructor?

开发者 https://www.devze.com 2023-01-31 07:40 出处:网络
I have a class, MyThread which implements Callable <String>. The class has a constructor which takesparam开发者_Go百科eters like a blockingqueue and others. In my main class, I instantiate this

I have a class, MyThread which implements Callable <String>. The class has a constructor which takes param开发者_Go百科eters 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.


You have effectively reinvented ThreadLocal. Enjoy!

0

精彩评论

暂无评论...
验证码 换一张
取 消