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();
}
}
标签:elements,get,lock,阻塞,len,CopyOnWriteArrayList,理解,newElements 来源: https://www.cnblogs.com/kltsee/p/14872889.html