CopyOnWriteArrayList 源码分析

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();
        }
    }

先上锁,再进行复制拷贝

你可能感兴趣的:(CopyOnWriteArrayList 源码分析)