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

ConcurrentHashMap and its operations

 
sandy sean
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Jhonson Fernando
MyExamCloud Software Support
Ranch Hand
Posts: 654
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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. "

 
sandy sean
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I could not understand it. will you please elaborate a little bit more on this with an example.
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 167
C++ Redhat Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21197
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Paul Clapham
Sheriff
Posts: 21133
32
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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".
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic