CopyOnWriteArrayList理解
get()没有阻塞,即使有线程正在添加元素也没有阻塞。
public E get(int index) {
return get(getArray(), index);
}
add()是阻塞的,
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();
}
}