[concurrency-interest] CopyOnWrite for Maps and other lists (not just ArrayList)

Kasper Nielsen kasper at kav.dk
Wed Dec 8 15:45:58 EST 2010

On 08-12-2010 19:52, Morgan Conrad wrote:
> --- On Wed, 12/8/10, Kasper Nielsen<kasper at kav.dk>  wrote:
>> The purpose of CopyOnWriteArrayList is to allow for fast
>> concurrent reads on the expense of fairly slow writes.
> I absolutely agree that, if you are happy with ArrayList behavior, which is true most of the time, that CopyOnWriteArrayList  is the way to go.
>> Your wrappers synchronizes all access to the underlying
>> Map. Except for your iterators with takes a snapshot of the
>> current data. CopyOnWriteArrayList does not take a snapshot
>> when iterating. Instead, it creates a new array when
>> somebody modifies it. Providing concurrent iterators access
>> to the same underlying array.
> My goals were decent speed, not ultimate speed.
May I ask you what use case you are trying to solve. I fail to see what 
kind of benefits your implementation provides over either
ConcurrentSkipListMap, ConcurrentHashMap or Collections.synchronizedMap?

> Maybe the read methods should not be synchronized???  Now that I think about it, they don't need to be synched because they don't affect the snapshot.  Right?
It is only your iterators that uses snapshots. So you cannot remove 
synchronized from map.get(), map.contains(), map.size()....


More information about the Concurrency-interest mailing list