[concurrency-interest] CopyOnWrite for Maps and other lists (not just ArrayList)
ben_manes at yahoo.com
Wed Dec 8 14:23:58 EST 2010
If you don't synchronize on reads and use wrappers then you're relying on the
caller to be careful when using collections that mutate on access (e.g.
LinkedHashMap). This is the same problem as Collections#synchronizedMap() and
friends, whereas the j.u.c. collections provide a safe collection that
encapsulates the decisions that make it thread safe. If you don't fully
synchronize your wrapper then you enable error-prone usages, which favors
adopting concrete implementations instead that are safe and optimized.
From: Morgan Conrad <morganconrad at yahoo.com>
To: Concurrency-interest at cs.oswego.edu; Kasper Nielsen <kasper at kav.dk>
Sent: Wed, December 8, 2010 10:52:26 AM
Subject: Re: [concurrency-interest] CopyOnWrite for Maps and other lists (not
--- 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?
Concurrency-interest mailing list
Concurrency-interest at cs.oswego.edu
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Concurrency-interest