Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Java in General and the fly likes ConcurrentHashMap in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "ConcurrentHashMap in java" Watch "ConcurrentHashMap in java" New topic
Author

ConcurrentHashMap in java

anish jain
Ranch Hand

Joined: Feb 03, 2010
Posts: 129
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

Joined: Jun 11, 2002
Posts: 103
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

Joined: Oct 29, 2012
Posts: 1
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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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.
 
Don't get me started about those stupid light bulbs.
 
subject: ConcurrentHashMap in java
 
Similar Threads
Synchronized and unsynchronized
Synchronizing a Hash Map accessed by two threads.
synchronization in vector
Most efficient Java collections
where we need ConcurrentHashMap ?????