I have a static method like follows
public static void foo(){
final ClassA a = new ClassA();
}
I have two hash maps inside classA.
Are those hash map thread safe…?
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.
It is not clear whether you mean to access the hash maps inside the object
awith multiple threads or call the methodfoowith multiple threads.In the first case thread safety of a collection has nothing to do with the outer context i.e. it was created in a static context or not. So no, if you try to access the hash maps from the object
awith multiple threads, it will cause unexpected behavior. You need to regulate access to them withsynchronizedblocks.In the second case, each thread will have a different copy of foo on its stack and allocate different instance of
ClassA. Therefore, they will not collide because they will have differentHashMapsto work with.Please clarify which of there cases you are working with.