[concurrency-interest] CopyOnWrite for Maps and other lists (not just ArrayList)
morganconrad at yahoo.com
Wed Dec 8 12:30:38 EST 2010
Thanks for the interest Chris!
A blog post, with links to the code, is at
The Source Code is stored on a wiki at
http://flyingspaniel.wikidot.com/cow (click on the files link at the bottom)
I took a brief look at the org.apache.mina.util.CopyOnWriteMap. It uses a HashMap underneath, and really does a copy on write of the underlying HashMap.
As mentioned in the blog post, strictly speaking, I do not "copy on write". I effectively "copy the iterators on write". In this way it supports (at least theoretically) any type of underlying List or Map, such as a TreeMap, where the order of iteration is much different than a HashMap, or some user's ReallyWeirdCustomCollection that has unusual rules for null keys or values, a custom Comparator, etc...
I believe the code to be reasonably thread-safe but can easily imagine members of this interest group finding holes or proposing improvements. :-)
I found it ironic/interesting that where java.util.concurrency uses actual classes for, say, CopyOnWriteArray, my code provides wrappers. And, while standard java.util.Collections provides wrappers for unmodifiable collections, my code, mainly to be different and provide an alternative, has classes.
Thanks for your interest, and I hope the code or idea proves useful to somebody. As things turned out, we didn't need to use this code, so it would be nice if somebody got some use out of it.
More information about the Concurrency-interest