Reputation: 455
I have created a hashmap outside my mutithreading code. There are going to be no changes in this hasmap later.
After this, I am starting two threads which will both be reading from this hashmap(yes, only read operations). If thread1 is reading from my hashmap object , can thread 2 also read at the same time? Or do I need a Concurrenthashmap or any other version of Map?
Upvotes: 0
Views: 971
Reputation: 2641
If you required only read operation then no need to use synchronization.If you are not doing any write after creation make it immutable so no body can change it .No need for synchronization.
Upvotes: 1
Reputation: 7720
If thread1 is reading from my hashmap object, can thread 2 also read at the same time?
If you are sure that there are no write operations then you need not use Synchronization options at all, Go for a normal version of Map.
You can also use Immutable Map
A Map whose contents will never change, with many other important properties detailed at ImmutableCollection
Upvotes: 2
Reputation: 2699
No you don't need concurrent Hash maps..but only since you don not do any modifications. You can read the same without any problem.
Upvotes: 2