[concurrency-interest] COWList snapshot.

Kasper Nielsen kasperni at gmail.com
Sat Feb 22 17:23:44 EST 2014


For consistency you might also want to add a CopyOnWriteArraySet.snapshot()?
Also unlike CopyOnWriteArrayList, CopyOnWriteArraySet does not implement
Cloneable.
Finally, CopyOnWriteArraySet.eq can be replaced by Objects.equals



On Fri, Feb 21, 2014 at 9:12 PM, Martin Buchholz <martinrb at google.com>wrote:

> 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.
>>>
>>
>
> _______________________________________________
> Concurrency-interest mailing list
> Concurrency-interest at cs.oswego.edu
> http://cs.oswego.edu/mailman/listinfo/concurrency-interest
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20140222/077d41eb/attachment.html>


More information about the Concurrency-interest mailing list