I do not understand why we need to put synchronized before getByName() since it is just reading from the map instead of modifying it.
How does getByName() modify shared resources?
Under discussion
Comments (3)
- Popular
- New
- Old
You must be signed in to leave a comment
Guadalupe Gagnon
20 November 2019, 14:59
https://stackoverflow.com/questions/18542037/how-to-prove-that-hashmap-in-java-is-not-thread-safe
Doing a little research I have found this post. It is basically saying that a HashMap is an array of buckets, with the keys being for the array and the values being the buckets. When you are adding/removing items to/from the Map, the internal array containing these buckets is automatically resized as needed. Errors would occur if a 'get' was called on the Map while this is occurring.
+10
Tony Roy
25 March 2020, 22:26
I had the same question as lonely gleaner. After puzzling on Guadalupe's answer for a while, it may help to add that the that "get" and "remove" synchronize on the *same* ApplicationContext object. So if we synchronize both methods and one is running, the other is not.
PS. Thanks for all the help you provide on this site.
+3
Guadalupe Gagnon
25 March 2020, 22:43
=)
0