my dog learned polymorphism*
The moose likes Threads and Synchronization and the fly likes ConcurrentHashMap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "ConcurrentHashMap" Watch "ConcurrentHashMap" New topic
Author

ConcurrentHashMap

sarah Marsh
Ranch Hand

Joined: Mar 06, 2001
Posts: 282
Hello,

As of ConcurrentHashMap: Since
Retrieval operations (including get) generally do not block, so may overlap with update operations (including put and remove).
Why
all operations are thread-safe?

Thanks!
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

The ConcurrentHashMap is broken up into small pieces called Segments that cover a specific range of Hashes. The Segments basically have final links to the next Node, so they can't change. Which means that a read can safely traverse from one Node to the next without worrying about interuption. When a modification makes a change to a Node it must remove the Node and insert a new one, which means it does not affect a Node that a retrieval is already using. It also always inserts the Node at the beginning of the link-list, meaning you are modifying the iteration of a get by inserting new elements as well.

Volatile variables help control where the iterations in the get occur.

Steve
 
GeeCON Prague 2014
 
subject: ConcurrentHashMap