Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Why use HashMap when you have ConcurrentHashMap ?  RSS feed

 
Pho Tek
Ranch Hand
Posts: 782
Chrome Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just finished a Google Android application last month and one of
the problems which I had encountered was handling ConcurrentModificationException.

Basically the app visualizes a graph in which the nodes of the graph could reside remotely.

Now the ConcurrentModificationException came about because as remote nodes were being added one by one into a HashMap; the UI was trying to render the nodes (via an Iterator). I replaced it with a ConcurrentHashMap (the problem went away of course) and I pondered on its correctness. My conclusion is that for my problem; it is sufficiently correct. The rationale: the UI is a graphical representation of the data in the Map. For every piece of data added to the Map; an onAdded event is fired to notify the UI widget to repaint. Assuming that repaint events are never lost (queued in the UI event thread); then it is guaranteed that the visual is a good enough snapshot. Note that any UI interaction via the D-PAD on the phone will also cause a repaint.

Unless you're writing some multithreaded realtime systems that require up-to-the-millisec correctness; it's not worth while to use HashMap. Is this a reasonable argument ?
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pho:
Unless you're writing some multithreaded realtime systems that require up-to-the-millisec correctness; it's not worth while to use HashMap. Is this a reasonable argument ?

I think it is as simple as this: If your HashMap stores shared data (accessed by different threads) then it has to be thread safe. I dont think anything changes with the performance requirements, as someone correctly said: "It is better to be safe than sorry". So, the idea is to make the HashMap threadsafe. Whether you choose a ConcurrentHashMap or your own custom implementation or Collections.synchronizedMap() is your choice. ConcurrentHashMap will perform better than most of the other implementations.

P.S.: Some discussion here for and against "ad-hoc" usage of ConcurrentHashMap.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HashMap is faster than ConcurrentHashMap. So if you aren't accessing data from multiple threads, or if the Map's data doesn't change at all after initialization, then you would usually want to use HashMap. Also, if you are using mulitple threads with a mutable Map, there are some cases where ConcurrentHashMap's methods are not enough, and you may require additional synchronization - e.g. if you want to iterate through the Map and guarantee that nothing gets changed while you are iterating. In that case, you're probably better off doing all the concurrency protection yourself through synchronization, rather than using ConcurrentHashMap. In which case again, you might as well use HashMap because it's faster. ConcurrentHashMap is the best solution for many concurrency problems with Maps, but not all.
 
Pho Tek
Ranch Hand
Posts: 782
Chrome Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
IMHO, there is one class of problem in which my "ad hoc" use of ConcurrentHashMap is correct. And that is when you're trying to visualize a collection of non-urgent data in a multithreaded situation. There is basically one producer thread (which receives input from the network) & a UI thread that renders the visualization. As new data trickles in; the producer thread will add it to the ConcurrentHashMap and notifies the UI thread to repaint. The UI thread just needs to get a snapshot of what's in the Map.

The producer thread is doing a putIfAbsent and the UI threads iterates.
And ConcurrentModificationException is a bad idea for this situation.

I agree with you that for performance reasons; doing the sync myself is the way to go.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!