[concurrency-interest] COWList snapshot.

Martin Buchholz martinrb at google.com
Fri Feb 21 15:12:14 EST 2014


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>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>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.
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20140221/d62c7baf/attachment.html>


More information about the Concurrency-interest mailing list