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

Morgan Conrad morganconrad at yahoo.com
Wed Dec 8 13:52:26 EST 2010



--- 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.  Synchronizing all the access was, I believe, necessary so that the snapshot taken while iterating is up to date.  (but you guys are more expert than me on that, and see below)  It also provides a "feature" that the underlying implementation need not be synchronized - I guess this should be noted.  Since the purpose is to make the underlying map "more concurrent", this seems to me like a good, at least a reasonable, feature.

Since writes are supposedly rare for a CopyOnWrite collection, my hope is that the synchronizes will not, in practice, be much of a performance hit.  I guess if you do lots of reads they could be a problem.  

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?



      


More information about the Concurrency-interest mailing list