I recently saw a piece of code which used a ThreadLocal object and kept a ConcurrentHashMap within it.
Is there any logic/benefit in this, or is it redundant?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If the only reference to the concurrent hashmap resides in the
ThreadLocal, the hashmap is obviously only referenced from a single thread. In such case I would say it is completely redundant.However, it’s not hard to imagine someone “sharing” the thread-locally stored hashmap with other threads: