From what I understand a concurrent hash map from Java 5 onwards gives you a thread safe hash map that does not use blocking access for iterators and updates (if a concurrency level is sufficient).
Given the following conditions:
would I be better off with simple hash maps?
My understanding is that I will probably be better off because my keys won't clash --that I can guarantee. But is it possible that the Java implementation will screw things up in the hash buckets by say assigning the same bucket to two different keys?
You should use ConcurrentHashMap when you need very high concurrency in your project. It is thread safe without synchronizing the whole map . Reads can happen very fast while write is done with a lock. There is no locking at the object level.
A ConcurrentHashMap has internal final class called Segment so we can say that ConcurrentHashMap is internally divided in segments of size 32, so at max 32 threads can work at a time.
HashMap is non-Synchronized in nature i.e. HashMap is not Thread-safe whereas ConcurrentHashMap is Thread-safe in nature. HashMap performance is relatively high because it is non-synchronized in nature and any number of threads can perform simultaneously.
ConcurrentHashMap is divided into different segments based on concurrency level. So different threads can access different segments concurrently in java. Can threads read the segment of ConcurrentHashMap locked by some other thread in java? Yes.
If you're inserting using multiple threads, even if the keys are different, you should definitely use ConcurrentHashMap
or synchronize the inserts. Plain HashMap
simply isn't safe for concurrent writes. Suppose two threads each need to expand the internal table at the same time... even though they're using different keys, that's a fundamentally problematic situation.
Now if you really really have good evidence that using a ConcurrentHashMap
for the rest of your application lifetime causes a problem (and I very much doubt that it does), you could perhaps build a concurrent hash map to start with, the convert it to a HashMap
(or even an immutable collection from Guava) in a single thread, making sure there's a happens-before barrier between "final map is published" and "threads read final map".
If you have independant keys across threads, you could consider independant Maps. If this is an option, each thread can have its own HashMap which doesn't need to be thread safe provided it is only used by one thread.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With