Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ConcurrentHashMap in java

 
anish jain
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I searched on net about ConcurrentHashMap but not able to understand it fully.

Could you please share any good link or explain about why to use ConcurrentHashMap and how it works?

 
Lanny Gilbert
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Performance is *usually* the reason you'd use a ConcurrentHashMap over a "regular" synchronized HashMap.
Why??

Because:

1.) Writing to a ConcurrentHashMap locks only a portion of the map;
2.) Reads can generally occur without locking.

HOWEVER, that doesn't mean you can just replace all of your HashMap variables with ConcurrentHashMap variables and your code will run
flawlessly. You need, at a minimum, to replace all the "puts" with putIfAbsent.
HIGHLY recommend Java Concurrency In Practice by Brian Goetz (and others).

Hope this helps!
 
Ram Satish Mokkapaty
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
anish jain wrote:I searched on net about ConcurrentHashMap but not able to understand it fully.

Could you please share any good link or explain about why to use ConcurrentHashMap and how it works?



All about ConcurrentHashMap:
http://javaopensourcecode.blogspot.com/2012/06/concurrenthashmap.html
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lanny Gilbert wrote:You need, at a minimum, to replace all the "puts" with putIfAbsent.


No, you wouldn't just blindly do that. The two have different semantics.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic