By Mektoub
via java.dzone.com
Published: May 31 2008 / 01:23
Last few months I have seen too much code where a HashMap (without any extra synchronization) is used instead of a thread-safe alternative like the ConcurrentHashMap or the less concurrent but still thread-safe HashTable.
Comments
Kirill Grouchnikov replied ago:
From the Javadocs of HashMap:
Note that this implementation is not synchronized. If multiple
threads access this map concurrently, and at least one of the threads
modifies the map structurally, it must be synchronized externally.
alarmnummer replied ago:
I don't understand your vote down.
It is true that it is explained in the Javadoc, and everyone that has a little bit of experience with concurrency knows that the HashMap should not be used in a multithreaded environment without any extra synchronization. But the problem is that still a lot of people are making this mistake.
The topic is not rocket science, but my hope is to remember people that the HashMap has is not save to use in a multi-threaded environment. My initial goal was not to place the item on Javalobby because for the more experienced developers it is completely old news. But apparently they thought it was 'good enough' so.. there it is.
Check my other blogposts for more interesting information about concurrency control.
ceaseoleo replied ago:
Hmm.. can I write an article about ArrayList not being synchronized. If you're a java developer and don't read the docs, you should be fired.
Voters For This Link (5)
Voters Against This Link (3)