[concurrency-interest] Re: Question about Hashtable andConcurrentHashMap.

David Holmes dholmes@dltech.com.au
Thu, 7 Oct 2004 17:17:37 +1000


<trimmed the cc to the interest group only>

> Paul Wagland writes:
> However, the following I am not sure is a reason... Surely though the 
> concurrent classes would not break the following? I mean, one can argue 
> about the style if desired, but replacing Hashtable with ConcurrentHashMap
> should not break this code? Or is there something really fundamental that 
> I am missing?
> 
> > Also, it's not uncommon for Java programmers to "synchronize" 
> > the hashtable object when they want to temp. lock the hashtable:
> >
> >   synchronized (hashtable) {
> >     hashtable.get ...
> >     hashtable.put ...
> >     // etc
> >   }

The code assumes that any individual method invocations by other threads are locked out for the duration of the synchronized block, so that the group of method invocations are applied atomically. That is not the case with ConcurrentHashMap which would allow any other method invocations to proceed concurrently, unless the caller also tried to lock the map first.

David Holmes