> OTOH, I guess there are some corner cases. For instances, making use of
> unsynchronised iterators.
>>> I came across usage of
>>> Collections.synchronizedMap(new ConcurrentHashMap())
[quoted text clipped - 9 lines]
> example? I mean, usually you would use CHM in order to get faster and
> more efficient synchronization as with synchronized, wouldn't you?
Consider the case when you want a lock on all of your writes, but not
your reads. Perhaps because constructing objects for writes interferes,
or you have to return a map that client code has some (partial)
expectation that it will be a synchronised map. You can use the wrapped
map for writes and the raw map for reads. However, that'd probably be
considerably confusing.
>> OTOH, I guess there are some corner cases. For instances, making use
>> of unsynchronised iterators.
>
> I am not sure what you mean by this.
In the original posting there is a synchronised loop. Map the map a
synchronised concurrent map, and you can lose that synchronised whilst
still being able to use the lock for multiple operations that should be
atomic (with regards to everything but that iterator).
Tom Hawtin
Robert Klemme - 21 Apr 2007 10:07 GMT
>>>> I came across usage of
>>>> Collections.synchronizedMap(new ConcurrentHashMap())
[quoted text clipped - 16 lines]
> map for writes and the raw map for reads. However, that'd probably be
> considerably confusing.
I see. That makes sense. Although in that case I'd rather use a
reentrant lock as this has shown to be faster than synchronized in my tests.
>>> OTOH, I guess there are some corner cases. For instances, making use
>>> of unsynchronised iterators.
[quoted text clipped - 5 lines]
> still being able to use the lock for multiple operations that should be
> atomic (with regards to everything but that iterator).
Yeah, basically a similar scenario as above: you use the CHM for "basic"
thread safety and add synchronization mechanisms for special scenarios /
methods / operations.
Thanks for clarifying!
Kind regards
robert