aspose file tools*
The moose likes Threads and Synchronization and the fly likes ConcurrentHashMap and its operations Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "ConcurrentHashMap and its operations" Watch "ConcurrentHashMap and its operations" New topic
Author

ConcurrentHashMap and its operations

sandy sean
Ranch Hand

Joined: Dec 30, 2010
Posts: 69
Suppose there is a ConcurrentHashMap and there are two threads.

Let both threads are reading some data from same bucket. then my understanding says that both can read that bucket concurrently as CHM does not block reading operations.

But suppose one thread is writing(put()) to a bucket then, can second thread simultaneously read(get()) from the same bucket or second thread will have to wait for the put operation to get completed?

If it were hastable then get() will have to wait until put operation gets completed But in case of CHM how it will behave?

Thanks...
koilraj abraham
Ranch Hand

Joined: Jun 27, 2011
Posts: 543
    
    2
Read the following statement from API.

"However, even though all operations are thread-safe, retrieval operations do not entail locking, and there is not any support for locking the entire table in a way that prevents all access. Retrieval operations (including get) generally do not block, so may overlap with update operations (including put and remove). Retrievals reflect the results of the most recently completed update operations holding upon their onset. "


Abraham
EPractize Labs Training Labs => OCMJEA 6 | OCAJP | OCPJP | OCEJWSD | OCEEJBD | OCEJPAD | OCEJWCD
sandy sean
Ranch Hand

Joined: Dec 30, 2010
Posts: 69
I could not understand it. will you please elaborate a little bit more on this with an example.
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3610
    
  60

It's particularly difficult to come up with examples that illustrate timing issues in multithreaded applications. I can't think of an easy example that would demonstrate that puts don't block gets in a ConcurrentHashMap. More importantly, examples are not conclusive in multithreaded applications. That some behavior didn't occur in an example doesn't mean it can't occur in general.

Assuming your question is:
sandy sean wrote:But suppose one thread is writing(put()) to a bucket then, can second thread simultaneously read(get()) from the same bucket or second thread will have to wait for the put operation to get completed?

then the answer would probably be
Citing the documentation, koilraj abraham wrote:Retrieval operations (including get) generally do not block, so may overlap with update operations (including put and remove).

(emphasis added)
Luan Cestari
Ranch Hand

Joined: Feb 07, 2010
Posts: 163

Just for curiosity, the Collections.synchronizedMap() ( http://docs.oracle.com/javase/7/docs/api/java/util/Collections.html#synchronizedMap(java.util.Map) http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/util/Collections.java#1951 ) does behave exactly what you thought about ConcurrentHashMap in first place (so, Collections.synchronizedMap() have a mutex lock for each operation, making only one thread use the map object at a time)


Please, visit me for some cool tech post at www.ourdailycodes.com
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3610
    
  60

Luan Cestari wrote:Just for curiosity, the Collections.synchronizedMap() ... does behave exactly what you thought about ConcurrentHashMap in first place

Just to clarify - the OP was aware that ConcurrentHashMap supports concurrent reads (gets). He asked whether reads are blocked by writes.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Martin Vajsar wrote:
Luan Cestari wrote:Just for curiosity, the Collections.synchronizedMap() ... does behave exactly what you thought about ConcurrentHashMap in first place

Just to clarify - the OP was aware that ConcurrentHashMap supports concurrent reads (gets). He asked whether reads are blocked by writes.



The ConcurrentHashMap class supports the concept of segmentation, which allows different segments to be worked upon separately. This allows parallel writes -- or even writes with reads. Unfortunately, this is an implementation detail, and is not documented to behave in a particular way. So, whether reads are blocked by writes? I guess the answer is that it depends.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18658
    
    8

We've already had the relevant part of the documentation posted:

Retrieval operations (including get) generally do not block, so may overlap with update operations (including put and remove).


The words "generally" and "may" indicate that, as Henry just said, the answer is "It depends".
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ConcurrentHashMap and its operations