加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

CopyOnWriteArrayList源码分析-java8

发布时间:2020-12-14 06:39:48 所属栏目:Java 来源:网络整理
导读:1.特性 此类是ArrayList的线程安全变体,其中所有更改操作(如add,set等)通过创建底层数组的最新副本来实现 迭代器特性: 迭代器使用快照方式,且在迭代期间数组不会改变,故不会出现并发异常。 迭代器创建后,对list对add,remove不会反映到迭代器中。 迭代

1.特性

  • 此类是ArrayList的线程安全变体,其中所有更改操作(如add,set等)通过创建底层数组的最新副本来实现
  • 迭代器特性:
    • 迭代器使用快照方式,且在迭代期间数组不会改变,故不会出现并发异常。
    • 迭代器创建后,对list对add,remove不会反映到迭代器中。
    • 迭代器自身不支持remove,set,add操作。这一点和其它集合的迭代器很不一样。
  • 允许元素为null。
  • 内存一致性 和其它并发集合一样,遵循happens-before原则,线程中的操作优先于将一个元素add进CopyOnWriteArrayList中。
  • 支持随机访问,浅拷贝,可序列化。
  • 使用了可重入锁ReentrantLock,保护了所有的修改操作。
  • 需要使用lock的操作类型 set,add,remove,clear,replace,sort,sublist相关的都需要。

    package sourcecode.analysis;
/**
 * @Author: cxh
 * @CreateTime: 18/4/12 11:02
 * @ProjectName: JavaBaseTest
 */

import java.util.*;
import java.util.List;
import java.util.ListIterator;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Consumer;
import java.util.function.Predicate;

/**
 * 此类是ArrayList的线程安全变体,其中所有更改操作(如add,set等)通过创建底层数组的最新副本来实现。
 *
 * 这通常代价很高,但是当遍历操作远远多余修改操作时,这样做效率就很高,并且当你不能or不想使用sychronize进行加锁时,* 这样做也很有用.
 * 快照样式的迭代器使用了一种迭代器被创建时获取数组的状态.在迭代器的生命周期内,这一数组不会改变,故不会出现并发异常.
 * 迭代器创建后,对list对add,remove不会反映到迭代器中.
 * 迭代器自身不支持remove,set,add操作.如果调用这样的操作会抛出异常.这一点和其它集合的迭代器很不一样.
 *
 * 允许元素为null.
 *
 * 内存一致性影响:和其它并发集合一样,线程中的操作优先于将一个元素add进CopyOnWriteArrayList中.
 * 这根据的是 happens-before原则.
 *
 * 此类实现了接口:List,RandomAccess,Cloneable,Serializable
 * 此类支持随机访问,浅拷贝,可序列化.
 *
 * @since 1.5
 * @author Doug Lea
 * @param <E> the type of elements held in this collection
 */
public class CopyOnWriteArrayList<E>
        implements java.util.List<E>,java.io.Serializable {
    private static final long serialVersionUID = 8673264195747942595L;

    //可重入锁保护了所有的修改操作.
    final transient ReentrantLock lock = new ReentrantLock();

    //数组就是array,对其访问智能通过getArray/setArray
    private transient volatile Object[] array;

    //获取数组.此方法没有被设定为private是为了在CopyOnWriteArraySet能对其进行调用.
    final Object[] getArray() {
        return array;
    }

    //设定数组为:传入参数a
    final void setArray(Object[] a) {
        array = a;
    }

    /*---------3个构造函数-------*/

    //创建一个空list
    public CopyOnWriteArrayList() {
        setArray(new Object[0]);
    }

    //创建一个list,它包含了指定集合的所有元素,元素顺序和传入集合的迭代器保持一致.
    public CopyOnWriteArrayList(Collection<? extends E> c) {
        Object[] elements;
        //如果传入集合就是此类的类型,则直接获取数组
        if (c.getClass() == CopyOnWriteArrayList.class)
            elements = ((CopyOnWriteArrayList<?>)c).getArray();
        else {
            //否则,先将集合c转换为数组
            elements = c.toArray();
            // c.toArray might (incorrectly) not return Object[] (see 6260652)
            //如果c.toArray返回类型不是Object[],则将其转为Object[]类型
            if (elements.getClass() != Object[].class)
                elements = Arrays.copyOf(elements,elements.length,Object[].class);//src,长度,新类型
        }
        setArray(elements);
    }

    //创建一个包含给定数组元素副本的list
    public CopyOnWriteArrayList(E[] toCopyIn) {
        setArray(Arrays.copyOf(toCopyIn,toCopyIn.length,Object[].class));
    }

    //返回数组长度
    public int size() {
        return getArray().length;
    }

    public boolean isEmpty() {
        return size() == 0;
    }

    //等价性比较
    private static boolean eq(Object o1,Object o2) {
        return (o1 == null) ? o2 == null : o1.equals(o2);
    }

    /**
     * 静态版本的indexOf,允许重复调用,而不需要每次都重新获取数组。[index,fence)
     * @param o element to search for
     * @param elements the array
     * @param index first index to search
     * @param fence one past last index to search
     * @return index of element,or -1 if absent
     */
    private static int indexOf(Object o,Object[] elements,int index,int fence) {
        if (o == null) {
            for (int i = index; i < fence; i++)
                if (elements[i] == null)
                    return i;
        } else {
            for (int i = index; i < fence; i++)
                if (o.equals(elements[i]))
                    return i;
        }
        return -1;
    }

    //除去索引含义,其它同上
    private static int lastIndexOf(Object o,int index) {
        if (o == null) {
            for (int i = index; i >= 0; i--)
                if (elements[i] == null)
                    return i;
        } else {
            for (int i = index; i >= 0; i--)
                if (o.equals(elements[i]))
                    return i;
        }
        return -1;
    }

    public boolean contains(Object o) {
        Object[] elements = getArray();
        return indexOf(o,elements,elements.length) >= 0;
    }

    public int indexOf(Object o) {
        Object[] elements = getArray();
        return indexOf(o,elements.length);
    }

    /**
     * @param e element to search for
     * @param index 查找开始索引
     */
    public int indexOf(E e,int index) {
        Object[] elements = getArray();
        return indexOf(e,index,elements.length);
    }


    public int lastIndexOf(Object o) {
        Object[] elements = getArray();
        return lastIndexOf(o,elements.length - 1);
    }

    public int lastIndexOf(E e,int index) {
        Object[] elements = getArray();
        return lastIndexOf(e,index);
    }

    //返回list的浅拷贝
    public Object clone() {
        try {
            @SuppressWarnings("unchecked")
            CopyOnWriteArrayList<E> clone =
                    (CopyOnWriteArrayList<E>) super.clone();
            //反序列化时,支持重置Lock
            clone.resetLock();
            return clone;
        } catch (CloneNotSupportedException e) {
            // this shouldn't happen,since we are Cloneable
            throw new InternalError();
        }
    }

    //返回一个新拷贝的数组
    public Object[] toArray() {
        Object[] elements = getArray();
        return Arrays.copyOf(elements,elements.length);
    }

   //和ArrayList一样
    @SuppressWarnings("unchecked")
    public <T> T[] toArray(T a[]) {
        Object[] elements = getArray();
        int len = elements.length;
        if (a.length < len)
            return (T[]) Arrays.copyOf(elements,len,a.getClass());
        else {
            System.arraycopy(elements,a,len);
            if (a.length > len)
                a[len] = null;
            return a;
        }
    }

    /*-----指定位置访问操作------*/

    @SuppressWarnings("unchecked")
    private E get(Object[] a,int index) {
        return (E) a[index];
    }

    public E get(int index) {
        return get(getArray(),index);
    }


    //修改值,需要调用lock,进行加锁
    public E set(int index,E element) {
        final ReentrantLock lock = this.lock;
        //加锁
        lock.lock();
        try {
            Object[] elements = getArray();
            E oldValue = get(elements,index);

            if (oldValue != element) {
                int len = elements.length;
                Object[] newElements = Arrays.copyOf(elements,len);
                newElements[index] = element;
                setArray(newElements);
            } else {
                // Not quite a no-op; ensures volatile write semantics
                setArray(elements);
            }
            return oldValue;
        } finally {
            //解锁
            lock.unlock();
        }
    }

    //添加元素,需要加锁lock
    public boolean add(E e) {
        final ReentrantLock lock = this.lock;
        //加锁
        lock.lock();
        try {
            Object[] elements = getArray();
            int len = elements.length;
            Object[] newElements = Arrays.copyOf(elements,len + 1);
            newElements[len] = e;
            setArray(newElements);
            return true;
        } finally {
            //解锁
            lock.unlock();
        }
    }

    //指定位置插入元素,使用lock
    public void add(int index,E element) {
        final ReentrantLock lock = this.lock;
        lock.lock();
        try {
            Object[] elements = getArray();
            int len = elements.length;
            if (index > len || index < 0)
                throw new IndexOutOfBoundsException("Index: "+index+
                        ",Size: "+len);
            Object[] newElements;
            int numMoved = len - index;
            if (numMoved == 0)
                newElements = Arrays.copyOf(elements,len + 1);
            else {
                newElements = new Object[len + 1];
                System.arraycopy(elements,newElements,index);
                System.arraycopy(elements,index + 1,numMoved);
            }
            newElements[index] = element;
            setArray(newElements);
        } finally {
            lock.unlock();
        }
    }

    //删除指定位置的元素,加锁lock
    public E remove(int index) {
        final ReentrantLock lock = this.lock;
        lock.lock();
        try {
            Object[] elements = getArray();
            int len = elements.length;
            E oldValue = get(elements,index);
            int numMoved = len - index - 1;
            if (numMoved == 0)
                setArray(Arrays.copyOf(elements,len - 1));
            else {
                Object[] newElements = new Object[len - 1];
                System.arraycopy(elements,numMoved);
                setArray(newElements);
            }
            return oldValue;
        } finally {
            lock.unlock();
        }
    }

    //根据快照,删除list第一个出现的元素o,加锁lock
    public boolean remove(Object o) {
        Object[] snapshot = getArray();//获取快照
        int index = indexOf(o,snapshot,snapshot.length);
        return (index < 0) ? false : remove(o,index);
    }
    //辅助方法
    private boolean remove(Object o,Object[] snapshot,int index) {
        final ReentrantLock lock = this.lock;
        lock.lock();
        try {
            Object[] current = getArray();
            int len = current.length;
            if (snapshot != current) findIndex: {
                int prefix = Math.min(index,len);
                for (int i = 0; i < prefix; i++) {
                    if (current[i] != snapshot[i] &amp;&amp; eq(o,current[i])) {
                        index = i;
                        break findIndex;
                    }
                }
                if (index >= len)
                    return false;
                if (current[index] == o)
                    break findIndex;
                index = indexOf(o,current,len);
                if (index < 0)
                    return false;
            }
            Object[] newElements = new Object[len - 1];
            System.arraycopy(current,index);
            System.arraycopy(current,len - index - 1);
            setArray(newElements);
            return true;
        } finally {
            lock.unlock();
        }
    }

    //删除指定范围元素,加锁lock
    void removeRange(int fromIndex,int toIndex) {
        final ReentrantLock lock = this.lock;
        lock.lock();
        try {
            Object[] elements = getArray();
            int len = elements.length;

            if (fromIndex < 0 || toIndex > len || toIndex < fromIndex)
                throw new IndexOutOfBoundsException();
            int newlen = len - (toIndex - fromIndex);
            int numMoved = len - toIndex;
            if (numMoved == 0)
                setArray(Arrays.copyOf(elements,newlen));
            else {
                Object[] newElements = new Object[newlen];
                System.arraycopy(elements,fromIndex);
                System.arraycopy(elements,toIndex,fromIndex,numMoved);
                setArray(newElements);
            }
        } finally {
            lock.unlock();
        }
    }

    //如果给定参数元素e不存在,则将其添加到list尾部.需要加锁lock
    public boolean addIfAbsent(E e) {
        Object[] snapshot = getArray();
        return indexOf(e,snapshot.length) >= 0 ? false :
                addIfAbsent(e,snapshot);
    }
    //辅助方法
    private boolean addIfAbsent(E e,Object[] snapshot) {
        final ReentrantLock lock = this.lock;
        lock.lock();
        try {
            Object[] current = getArray();
            int len = current.length;
            if (snapshot != current) {
                // Optimize for lost race to another addXXX operation
                int common = Math.min(snapshot.length,len);
                for (int i = 0; i < common; i++)
                    if (current[i] != snapshot[i] &amp;&amp; eq(e,current[i]))
                        return false;
                if (indexOf(e,common,len) >= 0)
                    return false;
            }
            Object[] newElements = Arrays.copyOf(current,len + 1);
            newElements[len] = e;
            setArray(newElements);
            return true;
        } finally {
            lock.unlock();
        }
    }

    public boolean containsAll(Collection<?> c) {
        Object[] elements = getArray();
        int len = elements.length;
        for (Object e : c) {
            if (indexOf(e,len) < 0)
                return false;
        }
        return true;
    }

    //删除list中有,c中也有的元素.加锁lock
    public boolean removeAll(Collection<?> c) {
        if (c == null) throw new NullPointerException();
        final ReentrantLock lock = this.lock;
        lock.lock();
        try {
            Object[] elements = getArray();
            int len = elements.length;
            if (len != 0) {
                // temp array holds those elements we know we want to keep
                int newlen = 0;
                Object[] temp = new Object[len];
                for (int i = 0; i < len; ++i) {
                    Object element = elements[i];
                    if (!c.contains(element))
                        temp[newlen++] = element;
                }
                if (newlen != len) {
                    setArray(Arrays.copyOf(temp,newlen));
                    return true;
                }
            }
            return false;
        } finally {
            lock.unlock();
        }
    }

    //保存list中,c中也有的元素,加锁lock
    public boolean retainAll(Collection<?> c) {
        if (c == null) throw new NullPointerException();
        final ReentrantLock lock = this.lock;
        lock.lock();
        try {
            Object[] elements = getArray();
            int len = elements.length;
            if (len != 0) {
                // temp array holds those elements we know we want to keep
                int newlen = 0;
                Object[] temp = new Object[len];
                for (int i = 0; i < len; ++i) {
                    Object element = elements[i];
                    if (c.contains(element))
                        temp[newlen++] = element;
                }
                if (newlen != len) {
                    setArray(Arrays.copyOf(temp,newlen));
                    return true;
                }
            }
            return false;
        } finally {
            lock.unlock();
        }
    }

    //将c中存在,且list中不存在的元素添加到list中,加锁lock
    public int addAllAbsent(Collection<? extends E> c) {
        Object[] cs = c.toArray();
        if (cs.length == 0)
            return 0;
        final ReentrantLock lock = this.lock;
        lock.lock();
        try {
            Object[] elements = getArray();
            int len = elements.length;
            int added = 0;
            // uniquify and compact elements in cs
            for (int i = 0; i < cs.length; ++i) {
                Object e = cs[i];
                if (indexOf(e,len) < 0 &amp;&amp;
                        indexOf(e,cs,added) < 0)
                    cs[added++] = e;
            }
            if (added > 0) {
                Object[] newElements = Arrays.copyOf(elements,len + added);
                System.arraycopy(cs,added);
                setArray(newElements);
            }
            return added;
        } finally {
            lock.unlock();
        }
    }

    //清除操作,加锁lock
    public void clear() {
        final ReentrantLock lock = this.lock;
        lock.lock();
        try {
            setArray(new Object[0]);
        } finally {
            lock.unlock();
        }
    }


    //将c中的的所有元素添加到list中,加锁lock
    public boolean addAll(Collection<? extends E> c) {
        Object[] cs = (c.getClass() == CopyOnWriteArrayList.class) ?
                ((CopyOnWriteArrayList<?>)c).getArray() : c.toArray();
        if (cs.length == 0)
            return false;
        final ReentrantLock lock = this.lock;
        lock.lock();
        try {
            Object[] elements = getArray();
            int len = elements.length;
            if (len == 0 &amp;&amp; cs.getClass() == Object[].class)
                setArray(cs);
            else {
                Object[] newElements = Arrays.copyOf(elements,len + cs.length);
                System.arraycopy(cs,cs.length);
                setArray(newElements);
            }
            return true;
        } finally {
            lock.unlock();
        }
    }

    //indx:插入第一个元素的索引,加锁lock
    public boolean addAll(int index,Collection<? extends E> c) {
        Object[] cs = c.toArray();
        final ReentrantLock lock = this.lock;
        lock.lock();
        try {
            Object[] elements = getArray();
            int len = elements.length;
            if (index > len || index < 0)
                throw new IndexOutOfBoundsException("Index: "+index+
                        ",Size: "+len);
            if (cs.length == 0)
                return false;
            int numMoved = len - index;
            Object[] newElements;
            if (numMoved == 0)
                newElements = Arrays.copyOf(elements,len + cs.length);
            else {
                newElements = new Object[len + cs.length];
                System.arraycopy(elements,index + cs.length,numMoved);
            }
            System.arraycopy(cs,cs.length);
            setArray(newElements);
            return true;
        } finally {
            lock.unlock();
        }
    }

    public void forEach(java.util.function.Consumer<? super E> action) {
        if (action == null) throw new NullPointerException();
        Object[] elements = getArray();
        int len = elements.length;
        for (int i = 0; i < len; ++i) {
            @SuppressWarnings("unchecked") E e = (E) elements[i];
            action.accept(e);
        }
    }

    //lock
    public boolean removeIf(java.util.function.Predicate<? super E> filter) {
        if (filter == null) throw new NullPointerException();
        final ReentrantLock lock = this.lock;
        lock.lock();
        try {
            Object[] elements = getArray();
            int len = elements.length;
            if (len != 0) {
                int newlen = 0;
                Object[] temp = new Object[len];
                for (int i = 0; i < len; ++i) {
                    @SuppressWarnings("unchecked") E e = (E) elements[i];
                    if (!filter.test(e))
                        temp[newlen++] = e;
                }
                if (newlen != len) {
                    setArray(Arrays.copyOf(temp,newlen));
                    return true;
                }
            }
            return false;
        } finally {
            lock.unlock();
        }
    }

    public void replaceAll(java.util.function.UnaryOperator<E> operator) {
        if (operator == null) throw new NullPointerException();
        final ReentrantLock lock = this.lock;
        lock.lock();
        try {
            Object[] elements = getArray();
            int len = elements.length;
            Object[] newElements = Arrays.copyOf(elements,len);
            for (int i = 0; i < len; ++i) {
                @SuppressWarnings("unchecked") E e = (E) elements[i];
                newElements[i] = operator.apply(e);
            }
            setArray(newElements);
        } finally {
            lock.unlock();
        }
    }

    //排序也需要加锁lock
    public void sort(Comparator<? super E> c) {
        final ReentrantLock lock = this.lock;
        lock.lock();
        try {
            Object[] elements = getArray();
            Object[] newElements = Arrays.copyOf(elements,elements.length);
            @SuppressWarnings("unchecked") E[] es = (E[])newElements;
            Arrays.sort(es,c);
            setArray(newElements);
        } finally {
            lock.unlock();
        }
    }

    //用于序列化
    private void writeObject(java.io.ObjectOutputStream s)
            throws java.io.IOException {

        s.defaultWriteObject();

        Object[] elements = getArray();
        // Write out array length
        s.writeInt(elements.length);

        // Write out all elements in the proper order.
        for (Object element : elements)
            s.writeObject(element);
    }

    //反序列化
    private void readObject(java.io.ObjectInputStream s)
            throws java.io.IOException,ClassNotFoundException {

        s.defaultReadObject();

        // bind to new lock
        //重置lock,用于反序列化
        resetLock();

        // Read in array length and allocate array
        int len = s.readInt();
        Object[] elements = new Object[len];

        // Read in all elements in the proper order.
        for (int i = 0; i < len; i++)
            elements[i] = s.readObject();
        setArray(elements);
    }

    public String toString() {
        return Arrays.toString(getArray());
    }

    public boolean equals(Object o) {
        if (o == this)
            return true;
        if (!(o instanceof java.util.List))
            return false;

        java.util.List<?> list = (java.util.List<?>)(o);
        Iterator<?> it = list.iterator();
        Object[] elements = getArray();
        int len = elements.length;
        for (int i = 0; i < len; ++i)
            if (!it.hasNext() || !eq(elements[i],it.next()))
                return false;
        if (it.hasNext())
            return false;
        return true;
    }

    //返回list的hashcode,记住计算方法.同ArrayList
    public int hashCode() {
        int hashCode = 1;
        Object[] elements = getArray();
        int len = elements.length;
        for (int i = 0; i < len; ++i) {
            Object obj = elements[i];
            hashCode = 31*hashCode + (obj==null ? 0 : obj.hashCode());
        }
        return hashCode;
    }

    //返回迭代器提供了list状态的快照.
    //迭代器不支持remove操作,遍历过程不需要额外同步操作.
    public Iterator<E> iterator() {
        return new COWIterator<E>(getArray(),0);
    }

    //返回迭代器提供了list状态的快照.
    //迭代器不支持remove,add操作,遍历过程不需要额外同步操作.
    public java.util.ListIterator<E> listIterator() {
        return new COWIterator<E>(getArray(),0);
    }

    //返回迭代器提供了list状态的快照.
    //迭代器不支持remove操作,遍历过程不需要额外同步操作.
    public java.util.ListIterator<E> listIterator(int index) {
        Object[] elements = getArray();
        int len = elements.length;
        if (index < 0 || index > len)
            throw new IndexOutOfBoundsException("Index: "+index);

        return new COWIterator<E>(elements,index);
    }

    /**
     * 分割器支持:Spliterator.IMMUTABLE,* Spliterator.ORDERED,Spliterator.SIZED,和 Spliterator.SUBSIZED.
     *
     * //返回迭代器提供了list状态的快照.遍历过程不需要额外同步操作.
     * @since 1.8
     */
    public Spliterator<E> spliterator() {
        return Spliterators.spliterator
                (getArray(),Spliterator.IMMUTABLE | Spliterator.ORDERED);
    }

    static final class COWIterator<E> implements java.util.ListIterator<E> {
        /** Snapshot of the array */
        private final Object[] snapshot;
        /** Index of element to be returned by subsequent call to next.  */
        private int cursor;

        private COWIterator(Object[] elements,int initialCursor) {
            cursor = initialCursor;
            snapshot = elements;
        }

        public boolean hasNext() {
            return cursor < snapshot.length;
        }

        public boolean hasPrevious() {
            return cursor > 0;
        }

        @SuppressWarnings("unchecked")
        public E next() {
            if (! hasNext())
                throw new NoSuchElementException();
            return (E) snapshot[cursor++];
        }

        @SuppressWarnings("unchecked")
        public E previous() {
            if (! hasPrevious())
                throw new NoSuchElementException();
            return (E) snapshot[--cursor];
        }

        public int nextIndex() {
            return cursor;
        }

        public int previousIndex() {
            return cursor-1;
        }

        /**
         * Not supported. Always throws UnsupportedOperationException.
         * @throws UnsupportedOperationException always; {@code remove}
         *         is not supported by this iterator.
         */
        public void remove() {
            throw new UnsupportedOperationException();
        }

        /**
         * Not supported. Always throws UnsupportedOperationException.
         * @throws UnsupportedOperationException always; {@code set}
         *         is not supported by this iterator.
         */
        public void set(E e) {
            throw new UnsupportedOperationException();
        }

        /**
         * Not supported. Always throws UnsupportedOperationException.
         * @throws UnsupportedOperationException always; {@code add}
         *         is not supported by this iterator.
         */
        public void add(E e) {
            throw new UnsupportedOperationException();
        }

        @Override
        public void forEachRemaining(java.util.function.Consumer<? super E> action) {
            Objects.requireNonNull(action);
            Object[] elements = snapshot;
            final int size = elements.length;
            for (int i = cursor; i < size; i++) {
                @SuppressWarnings("unchecked") E e = (E) elements[i];
                action.accept(e);
            }
            cursor = size;
        }
    }

    //list和sublist更新互相影响.加锁lock
    public java.util.List<E> subList(int fromIndex,int toIndex) {
        final ReentrantLock lock = this.lock;
        lock.lock();
        try {
            Object[] elements = getArray();
            int len = elements.length;
            if (fromIndex < 0 || toIndex > len || fromIndex > toIndex)
                throw new IndexOutOfBoundsException();
            return new COWSubList<E>(this,toIndex);
        } finally {
            lock.unlock();
        }
    }

    /**
     * CopyOnWriteArrayList截取的子list.
     * 为了方便,此类扩展了AbstractList,从而避免定义addAll等方法.这样做虽然没有什么危害性,但是却有点浪费.
     * 此类不需要使用modCount机制,但是需要使用类似的机制检查并发修改.
     * 在每一个操作中,list的底层数组都会被检查和更新.因为我们在所有定义在AbstractList中的方法都进行这项操作.而且都能正常工作.
     * 虽然这样做效率低下,但这并不值得改进.
     * 从AbstractList继承的列表操作在COW子列表上已经非常慢,以至于再多一点空间/时间也并不会产生明显影响。
     */
    private static class COWSubList<E>
            extends AbstractList<E>
            implements RandomAccess
    {
        private final CopyOnWriteArrayList<E> l;
        private final int offset;
        private int size;
        private Object[] expectedArray;

        // only call this holding l's lock,链表加锁lock时,才能调用这一方法
        COWSubList(CopyOnWriteArrayList<E> list,int fromIndex,int toIndex) {
            l = list;
            expectedArray = l.getArray();
            offset = fromIndex;
            size = toIndex - fromIndex;
        }

        // only call this holding l's lock,才能调用这一方法
        private void checkForComodification() {
            if (l.getArray() != expectedArray)
                throw new ConcurrentModificationException();
        }

        // only call this holding l's lock,才能调用这一方法
        private void rangeCheck(int index) {
            if (index < 0 || index >= size)
                throw new IndexOutOfBoundsException("Index: "+index+
                        ",Size: "+size);
        }

        public E set(int index,E element) {
            final ReentrantLock lock = l.lock;
            lock.lock();
            try {
                rangeCheck(index);
                checkForComodification();
                E x = l.set(index+offset,element);
                expectedArray = l.getArray();
                return x;
            } finally {
                lock.unlock();
            }
        }

        public E get(int index) {
            final ReentrantLock lock = l.lock;
            lock.lock();
            try {
                rangeCheck(index);
                checkForComodification();
                return l.get(index+offset);
            } finally {
                lock.unlock();
            }
        }

        public int size() {
            final ReentrantLock lock = l.lock;
            lock.lock();
            try {
                checkForComodification();
                return size;
            } finally {
                lock.unlock();
            }
        }

        public void add(int index,E element) {
            final ReentrantLock lock = l.lock;
            lock.lock();
            try {
                checkForComodification();
                if (index < 0 || index > size)
                    throw new IndexOutOfBoundsException();
                l.add(index+offset,element);
                expectedArray = l.getArray();
                size++;
            } finally {
                lock.unlock();
            }
        }

        public void clear() {
            final ReentrantLock lock = l.lock;
            lock.lock();
            try {
                checkForComodification();
                l.removeRange(offset,offset+size);
                expectedArray = l.getArray();
                size = 0;
            } finally {
                lock.unlock();
            }
        }

        public E remove(int index) {
            final ReentrantLock lock = l.lock;
            lock.lock();
            try {
                rangeCheck(index);
                checkForComodification();
                E result = l.remove(index+offset);
                expectedArray = l.getArray();
                size--;
                return result;
            } finally {
                lock.unlock();
            }
        }

        public boolean remove(Object o) {
            int index = indexOf(o);
            if (index == -1)
                return false;
            remove(index);
            return true;
        }

        public Iterator<E> iterator() {
            final ReentrantLock lock = l.lock;
            lock.lock();
            try {
                checkForComodification();
                return new COWSubListIterator<E>(l,offset,size);
            } finally {
                lock.unlock();
            }
        }

        public java.util.ListIterator<E> listIterator(int index) {
            final ReentrantLock lock = l.lock;
            lock.lock();
            try {
                checkForComodification();
                if (index < 0 || index > size)
                    throw new IndexOutOfBoundsException("Index: "+index+
                            ",Size: "+size);
                return new COWSubListIterator<E>(l,size);
            } finally {
                lock.unlock();
            }
        }

        public java.util.List<E> subList(int fromIndex,int toIndex) {
            final ReentrantLock lock = l.lock;
            lock.lock();
            try {
                checkForComodification();
                if (fromIndex < 0 || toIndex > size || fromIndex > toIndex)
                    throw new IndexOutOfBoundsException();
                return new COWSubList<E>(l,fromIndex + offset,toIndex + offset);
            } finally {
                lock.unlock();
            }
        }

        public void forEach(java.util.function.Consumer<? super E> action) {
            if (action == null) throw new NullPointerException();
            int lo = offset;
            int hi = offset + size;
            Object[] a = expectedArray;
            if (l.getArray() != a)
                throw new ConcurrentModificationException();
            if (lo < 0 || hi > a.length)
                throw new IndexOutOfBoundsException();
            for (int i = lo; i < hi; ++i) {
                @SuppressWarnings("unchecked") E e = (E) a[i];
                action.accept(e);
            }
        }

        public void replaceAll(java.util.function.UnaryOperator<E> operator) {
            if (operator == null) throw new NullPointerException();
            final ReentrantLock lock = l.lock;
            lock.lock();
            try {
                int lo = offset;
                int hi = offset + size;
                Object[] elements = expectedArray;
                if (l.getArray() != elements)
                    throw new ConcurrentModificationException();
                int len = elements.length;
                if (lo < 0 || hi > len)
                    throw new IndexOutOfBoundsException();
                Object[] newElements = Arrays.copyOf(elements,len);
                for (int i = lo; i < hi; ++i) {
                    @SuppressWarnings("unchecked") E e = (E) elements[i];
                    newElements[i] = operator.apply(e);
                }
                l.setArray(expectedArray = newElements);
            } finally {
                lock.unlock();
            }
        }

        public void sort(Comparator<? super E> c) {
            final ReentrantLock lock = l.lock;
            lock.lock();
            try {
                int lo = offset;
                int hi = offset + size;
                Object[] elements = expectedArray;
                if (l.getArray() != elements)
                    throw new ConcurrentModificationException();
                int len = elements.length;
                if (lo < 0 || hi > len)
                    throw new IndexOutOfBoundsException();
                Object[] newElements = Arrays.copyOf(elements,len);
                @SuppressWarnings("unchecked") E[] es = (E[])newElements;
                Arrays.sort(es,lo,hi,c);
                l.setArray(expectedArray = newElements);
            } finally {
                lock.unlock();
            }
        }

        public boolean removeAll(Collection<?> c) {
            if (c == null) throw new NullPointerException();
            boolean removed = false;
            final ReentrantLock lock = l.lock;
            lock.lock();
            try {
                int n = size;
                if (n > 0) {
                    int lo = offset;
                    int hi = offset + n;
                    Object[] elements = expectedArray;
                    if (l.getArray() != elements)
                        throw new ConcurrentModificationException();
                    int len = elements.length;
                    if (lo < 0 || hi > len)
                        throw new IndexOutOfBoundsException();
                    int newSize = 0;
                    Object[] temp = new Object[n];
                    for (int i = lo; i < hi; ++i) {
                        Object element = elements[i];
                        if (!c.contains(element))
                            temp[newSize++] = element;
                    }
                    if (newSize != n) {
                        Object[] newElements = new Object[len - n + newSize];
                        System.arraycopy(elements,lo);
                        System.arraycopy(temp,newSize);
                        System.arraycopy(elements,lo + newSize,len - hi);
                        size = newSize;
                        removed = true;
                        l.setArray(expectedArray = newElements);
                    }
                }
            } finally {
                lock.unlock();
            }
            return removed;
        }

        public boolean retainAll(Collection<?> c) {
            if (c == null) throw new NullPointerException();
            boolean removed = false;
            final ReentrantLock lock = l.lock;
            lock.lock();
            try {
                int n = size;
                if (n > 0) {
                    int lo = offset;
                    int hi = offset + n;
                    Object[] elements = expectedArray;
                    if (l.getArray() != elements)
                        throw new ConcurrentModificationException();
                    int len = elements.length;
                    if (lo < 0 || hi > len)
                        throw new IndexOutOfBoundsException();
                    int newSize = 0;
                    Object[] temp = new Object[n];
                    for (int i = lo; i < hi; ++i) {
                        Object element = elements[i];
                        if (c.contains(element))
                            temp[newSize++] = element;
                    }
                    if (newSize != n) {
                        Object[] newElements = new Object[len - n + newSize];
                        System.arraycopy(elements,len - hi);
                        size = newSize;
                        removed = true;
                        l.setArray(expectedArray = newElements);
                    }
                }
            } finally {
                lock.unlock();
            }
            return removed;
        }

        public boolean removeIf(Predicate<? super E> filter) {
            if (filter == null) throw new NullPointerException();
            boolean removed = false;
            final ReentrantLock lock = l.lock;
            lock.lock();
            try {
                int n = size;
                if (n > 0) {
                    int lo = offset;
                    int hi = offset + n;
                    Object[] elements = expectedArray;
                    if (l.getArray() != elements)
                        throw new ConcurrentModificationException();
                    int len = elements.length;
                    if (lo < 0 || hi > len)
                        throw new IndexOutOfBoundsException();
                    int newSize = 0;
                    Object[] temp = new Object[n];
                    for (int i = lo; i < hi; ++i) {
                        @SuppressWarnings("unchecked") E e = (E) elements[i];
                        if (!filter.test(e))
                            temp[newSize++] = e;
                    }
                    if (newSize != n) {
                        Object[] newElements = new Object[len - n + newSize];
                        System.arraycopy(elements,len - hi);
                        size = newSize;
                        removed = true;
                        l.setArray(expectedArray = newElements);
                    }
                }
            } finally {
                lock.unlock();
            }
            return removed;
        }

        public Spliterator<E> spliterator() {
            int lo = offset;
            int hi = offset + size;
            Object[] a = expectedArray;
            if (l.getArray() != a)
                throw new ConcurrentModificationException();
            if (lo < 0 || hi > a.length)
                throw new IndexOutOfBoundsException();
            return Spliterators.spliterator
                    (a,Spliterator.IMMUTABLE | Spliterator.ORDERED);
        }

    }

    private static class COWSubListIterator<E> implements java.util.ListIterator<E> {
        private final java.util.ListIterator<E> it;
        private final int offset;
        private final int size;

        COWSubListIterator(List<E> l,int offset,int size) {
            this.offset = offset;
            this.size = size;
            it = l.listIterator(index+offset);
        }

        public boolean hasNext() {
            return nextIndex() < size;
        }

        public E next() {
            if (hasNext())
                return it.next();
            else
                throw new NoSuchElementException();
        }

        public boolean hasPrevious() {
            return previousIndex() >= 0;
        }

        public E previous() {
            if (hasPrevious())
                return it.previous();
            else
                throw new NoSuchElementException();
        }

        public int nextIndex() {
            return it.nextIndex() - offset;
        }

        public int previousIndex() {
            return it.previousIndex() - offset;
        }

        public void remove() {
            throw new UnsupportedOperationException();
        }

        public void set(E e) {
            throw new UnsupportedOperationException();
        }

        public void add(E e) {
            throw new UnsupportedOperationException();
        }

        @Override
        public void forEachRemaining(Consumer<? super E> action) {
            Objects.requireNonNull(action);
            int s = size;
            ListIterator<E> i = it;
            while (nextIndex() < s) {
                action.accept(i.next());
            }
        }
    }

    // Support for resetting lock while deserializing
    private void resetLock() {
        UNSAFE.putObjectVolatile(this,lockOffset,new ReentrantLock());
    }
    private static final sun.misc.Unsafe UNSAFE;
    private static final long lockOffset;
    static {
        try {
            UNSAFE = sun.misc.Unsafe.getUnsafe();
            Class<?> k = CopyOnWriteArrayList.class;
            lockOffset = UNSAFE.objectFieldOffset
                    (k.getDeclaredField("lock"));
        } catch (Exception e) {
            throw new Error(e);
        }
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读