CopyOnWriteArrayList

2020-06-29  本文已影响0人  晨曦_lcq

ArrayList VS Vector VS CopyOnWriteArrayList

继承关系

CopyOnWriteArrayList.png

解析:

    final transient ReentrantLock lock = new ReentrantLock();
    // 元素
    private transient volatile Object[] array;
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();
        }
    }
``
上一篇下一篇

猜你喜欢

热点阅读