[concurrency-interest] COWList snapshot.

Jason Mehrens jason_mehrens at hotmail.com
Fri Feb 21 18:17:33 EST 2014


Martin,
Looks correct. Since the snapshot can't change I assume it should implement RandomAccess? Maybe override iterator() and listIterator() to return COWIterator for fun and profit?
As Henri pointed out, you can use Collections.unmodifiableList(Arrays.asList(getArray())). Arrays.asList doesn't perform any safe coping of the input array.

Jason
________________________________
> Date: Fri, 21 Feb 2014 12:12:14 -0800
> Subject: Re: [concurrency-interest] COWList snapshot.
> From: martinrb at google.com
> To: ppozerov at gmail.com
> CC: jason_mehrens at hotmail.com; concurrency-interest at cs.oswego.edu
>
> The v0.1 implementation was surprisingly easy:
>
> Index: src/main/java/util/concurrent/CopyOnWriteArrayList.java
> ===================================================================
> RCS file:
> /export/home/jsr166/jsr166/jsr166/src/main/java/util/concurrent/CopyOnWriteArrayList.java,v
> retrieving revision 1.115
> diff -u -r1.115 CopyOnWriteArrayList.java
> --- src/main/java/util/concurrent/CopyOnWriteArrayList.java 31 Jan 2014
> 17:58:35 -0000 1.115
> +++ src/main/java/util/concurrent/CopyOnWriteArrayList.java 21 Feb 2014
> 20:10:39 -0000
> @@ -1644,4 +1644,22 @@
> throw new Error(e);
> }
> }
> +
> + // Support for read-only snapshots
> + /**
> + * Returns an immutable list snapshot of this list.
> + * @return an immutable list snapshot of this list
> + */
> + public List<E> snapshot() {
> + return new Snapshot<E>(getArray());
> + }
> +
> + @SuppressWarnings("unchecked")
> + private static class Snapshot<E> extends AbstractList<E> {
> + private final Object[] elements;
> + public Snapshot(Object[] elements) { this.elements = elements; }
> + @Override public E get(int index) { return (E) elements[index]; }
> + @Override public int size() { return elements.length; }
> + @Override public Object[] toArray() { return elements.clone(); }
> + }
> }
>
>
>
> On Fri, Feb 21, 2014 at 11:41 AM, Martin Buchholz
> <martinrb at google.com<mailto:martinrb at google.com>> wrote:
> Summary: I support Владимир's suggestion of creating
> COWAList.snapshot(). I'm the obvious person to implement that, and I
> hope to do so sometime during jdk9 development.
>
>
>
> On Wed, Feb 19, 2014 at 11:55 AM, Martin Buchholz
> <martinrb at google.com<mailto:martinrb at google.com>> wrote:
> Владимир,
>
> I agree that a snapshot method as you propose would be useful. As
> always, implementing the List interface ends up being rather a lot of
> tedious work, which may be a reason it never got done.
> 		 	   		  


More information about the Concurrency-interest mailing list