Java: HashMap and concurrency

It is really surprising how many people ignore thread safety by justifying their incorrect solutions by saying “this thing is working, there are no issues and complains and it is fast”.

I’ve just encountered this kind of situation: a HashMap was used in a multithreaded environment where at any time following threads set could access it:
– at most one thread that was modifying the HashMap structurally
– some number of threads that were accessing the HashMap in read-only operations

Though I couldn’t reproduce any errors in this scenario using a HashMap in such a context is just invalid (believe me, M.Sc. in Computer Science, software engineering). When digging the case I found following interesting article: Java 7: HashMap vs ConcurrentHashMap

Moreover it presents ConcurrentHashMap data structure and presents some performance reports. I noticed people are afraid of this class because they suspect it is not fast enough. The above article shows ConcurrentHashMap is much more better than one could assume.

Another article showing ConcurrentHashMap is not only correct but also a fast solution: INFORMIT, Java Reference Guide: ConcurrentHashMap

About krzysztoftomaszewski

I've got M.Sc. in software engineering. I graduated in 2005 at Institute of Computer Science, Warsaw University of Technology, Faculty of Electronics and Information Technology. I'm working on computer software design and engineering continuously since 2004.
This entry was posted in Java, multithreading. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s