[concurrency-interest] ConcurrentHashMap - feature request

rganesan-l@myrealbox.com rganesan-l@myrealbox.com
23 Dec 2002 17:57:52 +0530


I am building a fairly complex data structure that needs an HashMap which
allows concurrency for both retrievals and updates. ConcurrentHashMap seems
to fit the bill exactly except for

There is NOT any support for locking the entire table to prevent
updates. This makes it imposssible, for example, to add an element only if
it is not already present, since another thread may be in the process of
doing the same thing. If you need such capabilities, consider instead using
the ConcurrentReaderHashMap class. 

Unfortunately, adding an element only if it is not already present is
exactly what I need. I cannot simply do a put and check for the old value
because old value is not the same as the new value. Using the
ConcurrentReaderHashMap is also not a viable option because there are
concurrent updaters.

It seems to be that what I need is a conditional put; something like 

public Object put(Object key, Object value, boolean replace)

When "replace" is true, this method behaves like the exiting put method;
when it's false it leaves the old value untouched. Looking at
ConcurrentHashMap.java, it appears that the API is quite easy to
implement. This API seems to be a useful addition even for
ConcurrentReaderHashMap. So, may be, what we need is a new interface 
on top of AbstractMap; I can't think of a good name - say CheckedAbstractMap.
Any comments?


Ganesan R