Roman
Roman

Reputation: 66196

ConcurrentHashMap: what's the point in locking updates only?

I always thought that ConcurrentHashMap and similar classes (which maintain synchronized updates and don't synchronize reads) did a very useful and intuitive thing: they did not lock reads and lock all functionality on updates. And a strategy like that does keep all things consistent.

But I read the documentation carefully, and opened the implementation of ConcurrentHashMap, and as I understand now, it does not block reads when another thread is performing updates. And if one thread starts doing putAll(hugeCollection) and another thread repeats contains(theSameObjectForAllCalls) at the same time then it's more then likely that the second thread gets different results while putAll is still working.

Here is the related part from the docs:

For aggregate operations such as putAll and clear, concurrent retrievals may reflect insertion or removal of only some entries.

Another interesting thing is that:

Retrievals reflect the results of the most recently completed update operations holding upon their onset.

This works not due to some locking, but because a new object is first being added and only after that objects counter is incremented and the object becomes visible for read operations.

So, what's the whole point of locking updates?

Upvotes: 7

Views: 2892

Answers (2)

nojo
nojo

Reputation: 1065

These classes give you additional control of tradeoffs around concurrency versus consistency. If what you really want is to have atomic, consistent reads, then get yourself a synchronized hashmap. However, if you need more concurrency, you can get a ConcurrentHashMap, so long as you're aware that reads will get an accurate picture of what the map looked like at some point, just not necessarily now.

The tradeoff is that you have to think harder about the consistency of the data, but your app can have much much greater concurrency.

Upvotes: 4

Faisal Feroz
Faisal Feroz

Reputation: 12785

Brian Goetz explained the working in an article at developer works. That should help out.

Upvotes: 4

Related Questions