手写偏门算法题 发表于 2021-03-15 | 分类于 面试算法题 | 字数统计: 83 | 阅读时长 ≈ 1 [TOC] CopyOnWriteArrayList123456789101112131415161718192021222324public class CopyOnWriteList<E> { ReentrantLock lock = new ReentrantLock(); int size = 0; private transient volatile Object[] array; public CopyOnWriteList() { this.array = new Object[0]; } public boolean add(E e) { final ReentrantLock lock = this.lock; lock.lock(); try { Object[] elements = this.array; int len = elements.length; Object[] newElements = Arrays.copyOf(elements, len + 1); newElements[len] = e; array = newElements; return true; } finally { lock.unlock(); } }}