----- Original message -----
> Gregg Wonderly wrote:
> > On 2/8/2011 10:51 AM, Patricia Shanahan wrote:
> > > There are some negatives to using CHM compared to wrapping a HashMap in a
> > > synchronized map. It is easy to write methods for compound operations
> > > such as
> > > removing a key-value pair if, and only if, some other key is present.
> >
> > Yes, this is true.  If we do need some compound operations, then it
> > would be necessary to add a lock to synchronize on, and then everything
> > else referencing the structure would need to be synchronized since CHM
> > uses internal locking which does not involve synchronized(this).
>
> Or switch implementations if there is a problem with the initial choice.
> If the code uses the Map interface, implemented by both CHM and a
> synchronized map wrapping a HashMap, it should just be a matter of
> changing the initialization expression.
>
> Patricia

Well almost, put if absent requires a check on the return value, often you need 
to create a new object, then do a put if absent, if there was an intervening 
put, throw away the new object and instead use the existing one.

The synchronised collections wrappers are directly substitutable, I've created 
similar multi read single write wrappers that are directly substitutable, these 
also return a snapshot iterator that won't throw a concurrent modification 
exception.

The good part about using the wrappers is that concurrency concerns are not 
reimplemented in each class, instead you've only got one place to do 
concurrency debugging and maintenance.

Peter.

Reply via email to