Link Details

Link 83903 thumbnail
User 224222 avatar

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.
  • 5
  • 3
  • 1487
  • 341

Comments

Add your comment
User 160542 avatar

Kirill Grouchnikov replied ago:

0 votes Vote down Vote up Reply

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.

User 160967 avatar

alarmnummer replied ago:

0 votes Vote down Vote up Reply

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.

User 283139 avatar

ceaseoleo replied ago:

0 votes Vote down Vote up Reply

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.

Add your comment


Html tags not supported. Reply is editable for 5 minutes. Use [code lang="java|ruby|sql|css|xml"][/code] to post code snippets.

Voters For This Link (5)



Voters Against This Link (3)