目录
3、get / indexOf / lastIndexOf / contains
6、remove / removeAll / removeIf / removeRange
7、replaceAll / retainAll / clear
CopyOnWriteArrayList表示一个写时复制的基于数组实现的线程安全的List实现类,写时复制是指如果修改数组元素,则会在当前数组的基础上复制一个新数组,所有的修改都在新数组中完成,修改完成后再替换到原来的数组,保证修改时不影响原数组的读操作,注意所有的写操作都需要加锁,而读操作不需要获取锁;CopyOnWriteArraySet是基于CopyOnWriteArrayList实现的一个线程安全的Set实现类,本篇博客就详细探讨这两个类的实现细节。
一、CopyOnWriteArraySet
CopyOnWriteArraySet的类继承关系如下:
其核心接口Set的典型实现类就是基于HashMap实现的HashSet,是非线程安全的, CopyOnWriteArraySet的实现基于CopyOnWriteArrayList,其关键方法实现如下:
public CopyOnWriteArraySet() {
al = new CopyOnWriteArrayList<E>();
}
public boolean contains(Object o) {
return al.contains(o);
}
public boolean remove(Object o) {
return al.remove(o);
}
public boolean add(E e) {
return al.addIfAbsent(e);
}
public Iterator<E> iterator() {
return al.iterator();
}
二、CopyOnWriteArrayList
1、定义
CopyOnWriteArrayList的类继承关系如下:
其中RandomAccess是一个标记类的空接口,表示该类支持随机访问,其核心接口List的典型实现类有基于数组实现的ArrayList和Vector,基于链表实现的LinkedList,除Vector是线程安全的外,另外两个都是线程不安全的,不过Vector的线程安全是通过对修改方法加synchronized实现的,锁的粒度太粗,并发性能低,不推荐使用。该类包含的属性如下:
/** 互斥锁 */
final transient ReentrantLock lock = new ReentrantLock();
/**实际保存元素的数组,注意array是volatile 变量 */
private transient volatile Object[] array;
除此之外,还有两个静态属性,通过static代码块初始化,如下:
2、构造方法
public CopyOnWriteArrayList() {
//初始数组长度为0
setArray(new Object[0]);
}
public CopyOnWriteArrayList(Collection<? extends E> c) {
Object[] elements;
if (c.getClass() == CopyOnWriteArrayList.class)
elements = ((CopyOnWriteArrayList<?>)c).getArray();
else {
elements = c.toArray();
if (elements.getClass() != Object[].class)
//进行类型转换
elements = Arrays.copyOf(elements, elements.length, Object[].class);
}
setArray(elements);
}
public CopyOnWriteArrayList(E[] toCopyIn) {
setArray(Arrays.copyOf(toCopyIn, toCopyIn.length, Object[].class));
}
public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) {
@SuppressWarnings("unchecked")
T[] copy = ((Object)newType == (Object)Object[].class) //如果是Object数组
? (T[]) new Object[newLength]
: (T[]) Array.newInstance(newType.getComponentType(), newLength); //如果不是Object数组,使用指定类型创建一个新数组
//数组复制
System.arraycopy(original, 0, copy, 0,
Math.min(original.length, newLength));
return copy;
}
3、get / indexOf / lastIndexOf / contains
所有的读取操作都是读取的array的一个副本,而不一定是当前的array,即读取的过程中array可能被修改了,但是此时读取操作是感知不到的,只有下次重新读取array时才知道。
//读取指定位置的元素
public E get(int index) {
return get(getArray(), index);
}
//获取指定元素的索引,从头开始查找
public int indexOf(Object o) {
Object[] elements = getArray();
return indexOf(o, elements, 0, elements.length);
}
//同上,不过从指定索引处开始查找
public int indexOf(E e, int index) {
Object[] elements = getArray();
return indexOf(e, elements, index, elements.length);
}
//从数组的末端开始往前查找
public int lastIndexOf(Object o) {
Object[] elements = getArray();
return lastIndexOf(o, elements, elements.length - 1);
}
//从数组的指定位置开始往前查找
public int lastIndexOf(E e, int index) {
Object[] elements = getArray();
return lastIndexOf(e, elements, index);
}
public boolean contains(Object o) {
Object[] elements = getArray();
return indexOf(o, elements, 0, elements.length) >= 0;
}
final Object[] getArray() {
return array;
}
public E get(int index) {
return get(getArray(), index);
}
//index是查找的起始索引,fence是查找的终止索引
private static int indexOf(Object o, Object[] elements,
int index, int fence) {
//从index往后遍历直到fence,如果找到目标节点,则返回其索引
if (o == null) {
for (int i = index; i < fence; i++)
if (elements[i] == null)
return i;
} else {
for (int i = index; i < fence; i++)
if (o.equals(elements[i]))
return i;
}
return -1;
}
//index是查找的终止索引
private static int lastIndexOf(Object o, Object[] elements, int index) {
//从index开始往前遍历
if (o == null) {
for (int i = index; i >= 0; i--)
if (elements[i] == null)
return i;
} else {
for (int i = index; i >= 0; i--)
if (o.equals(elements[i]))
return i;
}
return -1;
}
4、add / set / addIfAbsent
所有的修改方法都会加锁,在原数组的基础上复制出一个新数组,在新数组中修改,然后修改array属性指向新数组,从而不影响正在访问老数组的读操作。
//插入到末尾
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;
//更新array
setArray(newElements);
return true;
} finally {
lock.unlock();
}
}
//插入到指定位置
public void add(int index, E element) {
final ReentrantLock lock = this.lock;
lock.lock();
try {
Object[] elements = getArray();
int len = elements.length;
if (index > len || index < 0) //指定索引越界非法
throw new IndexOutOfBoundsException("Index: "+index+
", Size: "+len);
Object[] newElements;
int numMoved = len - index; //计算需要移动的元素个数
if (numMoved == 0)
newElements = Arrays.copyOf(elements, len + 1);
else {
newElements = new Object[len + 1];
//复制index之前的元素
System.arraycopy(elements, 0, newElements, 0, index);
//复制index之后的元素,新数组的起始位置是index + 1,index就空出来了
System.arraycopy(elements, index, newElements, index + 1,
numMoved);
}
//保存元素
newElements[index] = element;
//更新array
setArray(newElements);
} finally {
lock.unlock();
}
}
//如果指定的元素不存在才添加,不是List接口定义的方法
public boolean addIfAbsent(E e) {
Object[] snapshot = getArray();
//indexOf找到目标元素的索引,如果已存在,返回false,否则插入到数组中
return indexOf(e, snapshot, 0, snapshot.length) >= 0 ? false :
addIfAbsent(e, snapshot);
}
//修改指定位置的元素,如果与原来的值相同则不变,否则创建一个新数组
public E set(int index, E element) {
final ReentrantLock lock = this.lock;
lock.lock();
try {
Object[] elements = getArray();
//获取原来的value
E oldValue = get(elements, index);
if (oldValue != element) {
//如果不同,则复制一个新数组
int len = elements.length;
Object[] newElements = Arrays.copyOf(elements, len);
//保存元素 ,更新数组
newElements[index] = element;
setArray(newElements);
} else {
//已经加锁,array不会改变,重新设置
setArray(elements);
}
return oldValue;
} finally {
lock.unlock();
}
}
private boolean addIfAbsent(E e, Object[] snapshot) {
final ReentrantLock lock = this.lock;
lock.lock();
try {
Object[] current = getArray();
int len = current.length;
if (snapshot != current) { //说明数组发生了修改
//取两者的最小值
int common = Math.min(snapshot.length, len);
//遍历现在的数组,是否存在相同的元素,如果存在则返回false
for (int i = 0; i < common; i++)
if (current[i] != snapshot[i] && eq(e, current[i]))
return false;
//查找common后的部分是否存在目标元素
if (indexOf(e, current, common, len) >= 0)
return false;
}
//不存在目标元素,复制一个新的数组
Object[] newElements = Arrays.copyOf(current, len + 1);
//添加元素,更新数组
newElements[len] = e;
setArray(newElements);
return true;
} finally {
lock.unlock();
}
}
final void setArray(Object[] a) {
array = a;
}
5、addAll / addAllAbsent
//将集合c中的元素添加到数组末端
public boolean addAll(Collection<? extends E> c) {
Object[] cs = (c.getClass() == CopyOnWriteArrayList.class) ?
((CopyOnWriteArrayList<?>)c).getArray() : c.toArray();
if (cs.length == 0)
return false;
final ReentrantLock lock = this.lock;
lock.lock();
try {
Object[] elements = getArray();
int len = elements.length;
if (len == 0 && cs.getClass() == Object[].class) //原来是空,且两者类型一致,直接修改array
setArray(cs);
else {
//复制新数组
Object[] newElements = Arrays.copyOf(elements, len + cs.length);
//将目标数组复制到新数组中
System.arraycopy(cs, 0, newElements, len, cs.length);
setArray(newElements);
}
return true;
} finally {
lock.unlock();
}
}
//将集合c中的元素添加到数组指定索引处
public boolean addAll(int index, Collection<? extends E> c) {
Object[] cs = c.toArray();
final ReentrantLock lock = this.lock;
lock.lock();
try {
Object[] elements = getArray();
int len = elements.length;
if (index > len || index < 0) //index越界非法
throw new IndexOutOfBoundsException("Index: "+index+
", Size: "+len);
if (cs.length == 0) //c是空的
return false;
//计算需要移动的元素个数
int numMoved = len - index;
Object[] newElements;
if (numMoved == 0) //不需要移动
newElements = Arrays.copyOf(elements, len + cs.length);
else {
//需要移动
newElements = new Object[len + cs.length];
//分别拷贝index之前和之后的元素,将中间的位置空出来
System.arraycopy(elements, 0, newElements, 0, index);
System.arraycopy(elements, index,
newElements, index + cs.length,
numMoved);
}
//拷贝目标数组的元素到新数组
System.arraycopy(cs, 0, newElements, index, cs.length);
setArray(newElements);
return true;
} finally {
lock.unlock();
}
}
//将集合c中的元素添加到List中,只有目标元素不在List中才添加,返回添加的元素个数
public int addAllAbsent(Collection<? extends E> c) {
Object[] cs = c.toArray();
if (cs.length == 0)
return 0;
final ReentrantLock lock = this.lock;
lock.lock();
try {
Object[] elements = getArray();
int len = elements.length;
int added = 0;
//遍历cs中的元素
for (int i = 0; i < cs.length; ++i) {
Object e = cs[i];
if (indexOf(e, elements, 0, len) < 0 && //e在elements中不存在
indexOf(e, cs, 0, added) < 0) //e之前没有添加过
cs[added++] = e;
}
if (added > 0) { //c中有elements中不存在的元素
//复制一个新数组,将cs中added前的元素拷贝过去
Object[] newElements = Arrays.copyOf(elements, len + added);
System.arraycopy(cs, 0, newElements, len, added);
setArray(newElements);
}
//返回添加的元素数量
return added;
} finally {
lock.unlock();
}
}
6、remove / removeAll / removeIf / removeRange
//移除指定索引的元素
public E remove(int index) {
final ReentrantLock lock = this.lock;
lock.lock();
try {
Object[] elements = getArray();
int len = elements.length;
//获取该索引原来的值
E oldValue = get(elements, index);
//计算需要移动的元素个数
int numMoved = len - index - 1;
if (numMoved == 0) //不需要移动
setArray(Arrays.copyOf(elements, len - 1));
else {
Object[] newElements = new Object[len - 1];
//拷贝指定index之前和之后的元素
System.arraycopy(elements, 0, newElements, 0, index);
System.arraycopy(elements, index + 1, newElements, index,
numMoved);
setArray(newElements);
}
return oldValue;
} finally {
lock.unlock();
}
}
//移除指定的元素,返回是否移除成功
public boolean remove(Object o) {
Object[] snapshot = getArray();
//找到目标元素的索引
int index = indexOf(o, snapshot, 0, snapshot.length);
//如果不存在返回false,否则移除指定索引处的元素
return (index < 0) ? false : remove(o, snapshot, index);
}
//移除指定索引范围的元素
void removeRange(int fromIndex, int toIndex) {
final ReentrantLock lock = this.lock;
lock.lock();
try {
Object[] elements = getArray();
int len = elements.length;
//参数非法
if (fromIndex < 0 || toIndex > len || toIndex < fromIndex)
throw new IndexOutOfBoundsException();
//新数组的长度
int newlen = len - (toIndex - fromIndex);
//需要移动的数组元素的个数
int numMoved = len - toIndex;
if (numMoved == 0) //不需要移动,即移除的是末端的元素
setArray(Arrays.copyOf(elements, newlen));
else {
//需要移动
Object[] newElements = new Object[newlen];
//复制fromIndex之前的元素
System.arraycopy(elements, 0, newElements, 0, fromIndex);
//复制toIndex之后的元素
System.arraycopy(elements, toIndex, newElements,
fromIndex, numMoved);
setArray(newElements);
}
} finally {
lock.unlock();
}
}
//如果数组中的元素位于集合c中则移除
public boolean removeAll(Collection<?> c) {
if (c == null) throw new NullPointerException();
final ReentrantLock lock = this.lock;
lock.lock();
try {
Object[] elements = getArray();
int len = elements.length;
if (len != 0) {
// temp array holds those elements we know we want to keep
int newlen = 0;
Object[] temp = new Object[len];
//遍历当前的数组
for (int i = 0; i < len; ++i) {
Object element = elements[i];
if (!c.contains(element)) //如果c中不包含特定元素,保存该元素到temp中
temp[newlen++] = element;
}
if (newlen != len) { //如果移除了部分元素
//复制一个新数组,因为temp数组的长度是len
setArray(Arrays.copyOf(temp, newlen));
return true;
}
}
return false;
} finally {
lock.unlock();
}
}
//index是元素o在snapshot中的索引
private boolean remove(Object o, Object[] snapshot, int index) {
final ReentrantLock lock = this.lock;
lock.lock();
try {
Object[] current = getArray();
int len = current.length;
//因为之前查找未加锁,所以查找时的数组跟当前数组可能不一致
if (snapshot != current) findIndex: {
//取index和当前数组长度的最小值
int prefix = Math.min(index, len);
for (int i = 0; i < prefix; i++) {
//同一索引处两者元素不一致,且当前数组中的元素就是目标元素
//如果是一致的,则index不变,通过下面的第二个if分支可以检验
if (current[i] != snapshot[i] && eq(o, current[i])) {
index = i;
break findIndex; //会终止for循环和if分支内后面代码的执行
}
}
if (index >= len) //index大于当前数组的长度,肯定不存在
return false;
if (current[index] == o) //current数组中index处的元素还是o
break findIndex; //会终止if分支内后面代码的执行
//在current中查找该元素
index = indexOf(o, current, index, len);
if (index < 0)
return false; //不存在返回false
}
//创建新数组,分别复制index之前和之后的元素到新数组中
Object[] newElements = new Object[len - 1];
System.arraycopy(current, 0, newElements, 0, index);
System.arraycopy(current, index + 1,
newElements, index,
len - index - 1);
setArray(newElements);
return true;
} finally {
lock.unlock();
}
}
private static boolean eq(Object o1, Object o2) {
return (o1 == null) ? o2 == null : o1.equals(o2);
}
7、replaceAll / retainAll / clear
//遍历数组元素,将其作为UnaryOperator的入参,将结果保存到同一索引处,即替换掉原来的元素
public void replaceAll(UnaryOperator<E> operator) {
if (operator == null) throw new NullPointerException();
final ReentrantLock lock = this.lock;
lock.lock();
try {
Object[] elements = getArray();
int len = elements.length;
//复制一个新数组
Object[] newElements = Arrays.copyOf(elements, len);
//遍历老数组的元素,将计算结果保存到新数组中
for (int i = 0; i < len; ++i) {
@SuppressWarnings("unchecked") E e = (E) elements[i];
newElements[i] = operator.apply(e);
}
setArray(newElements);
} finally {
lock.unlock();
}
}
//将当前数组中不在集合c中的元素删除掉,即取两者的交集
public boolean retainAll(Collection<?> c) {
if (c == null) throw new NullPointerException();
final ReentrantLock lock = this.lock;
lock.lock();
try {
Object[] elements = getArray();
int len = elements.length;
if (len != 0) {
// temp array holds those elements we know we want to keep
int newlen = 0;
Object[] temp = new Object[len];
//遍历当前数组的元素
for (int i = 0; i < len; ++i) {
Object element = elements[i];
if (c.contains(element)) //如果在集合c中,则保存起来
temp[newlen++] = element;
}
if (newlen != len) { //部分元素不同
//复制一个新数组
setArray(Arrays.copyOf(temp, newlen));
return true;
}
}
return false;
} finally {
lock.unlock();
}
}
//清空数组
public void clear() {
final ReentrantLock lock = this.lock;
lock.lock();
try {
setArray(new Object[0]);
} finally {
lock.unlock();
}
}
8、iterator / listIterator
这两方法都是遍历,只不过返回的遍历器接口不同,如下:
public Iterator<E> iterator() {
//遍历的数组是array的副本
return new COWIterator<E>(getArray(), 0);
}
public ListIterator<E> listIterator() {
return new COWIterator<E>(getArray(), 0);
}
//从指定索引处开始遍历
public ListIterator<E> listIterator(int index) {
Object[] elements = getArray();
int len = elements.length;
if (index < 0 || index > len) //数组越界
throw new IndexOutOfBoundsException("Index: "+index);
return new COWIterator<E>(elements, index);
}
ListIterator继承自Iterator,在其基础上添加了如下方法:
内部类COWIterator的实现如下:
static final class COWIterator<E> implements ListIterator<E> {
/** 数组快照,即array的副本*/
private final Object[] snapshot;
/**下一次next方法返回元素的索引 */
private int cursor;
private COWIterator(Object[] elements, int initialCursor) {
cursor = initialCursor;
snapshot = elements;
}
public boolean hasNext() {
return cursor < snapshot.length;
}
public boolean hasPrevious() {
return cursor > 0;
}
@SuppressWarnings("unchecked")
public E next() {
if (! hasNext())
throw new NoSuchElementException();
//先返回cursor对应的元素,再加1
return (E) snapshot[cursor++];
}
@SuppressWarnings("unchecked")
public E previous() {
if (! hasPrevious())
throw new NoSuchElementException();
//先减1,在返回减后的结果对应的元素
return (E) snapshot[--cursor];
}
public int nextIndex() {
return cursor;
}
public int previousIndex() {
return cursor-1;
}
//因为遍历的是副本,所以修改是无意义的
public void remove() {
throw new UnsupportedOperationException();
}
public void set(E e) {
throw new UnsupportedOperationException();
}
public void add(E e) {
throw new UnsupportedOperationException();
}
@Override
public void forEachRemaining(Consumer<? super E> action) {
Objects.requireNonNull(action);
Object[] elements = snapshot;
final int size = elements.length;
//遍历剩余的元素
for (int i = cursor; i < size; i++) {
@SuppressWarnings("unchecked") E e = (E) elements[i];
action.accept(e);
}
cursor = size;
}
}
9、subList
subList返回当前List的指定范围的索引的子List,其实现如下:
public List<E> subList(int fromIndex, int toIndex) {
final ReentrantLock lock = this.lock;
lock.lock();
try {
Object[] elements = getArray();
int len = elements.length;
//校验参数合法
if (fromIndex < 0 || toIndex > len || fromIndex > toIndex)
throw new IndexOutOfBoundsException();
//将this传递进去
return new COWSubList<E>(this, fromIndex, toIndex);
} finally {
lock.unlock();
}
}
COWSubList(CopyOnWriteArrayList<E> list,
int fromIndex, int toIndex) {
l = list;
expectedArray = l.getArray();
offset = fromIndex;
size = toIndex - fromIndex;
}
COWSubList是一个内部类,也实现了List接口,不过其接口实现都是基于父List提供的方法,关键方法的实现如下:
public E get(int index) {
final ReentrantLock lock = l.lock;
lock.lock();
try {
//检查index是否越界
rangeCheck(index);
//检查当前数组是否变更
checkForComodification();
//index是相当于SubList的,相对于父List就需要加上offset
return l.get(index+offset);
} finally {
lock.unlock();
}
}
public E set(int index, E element) {
final ReentrantLock lock = l.lock;
lock.lock();
try {
rangeCheck(index);
checkForComodification();
E x = l.set(index+offset, element);
//上述set操作会更新array,所以这里重新读取array
expectedArray = l.getArray();
return x;
} finally {
lock.unlock();
}
}
public void add(int index, E element) {
final ReentrantLock lock = l.lock;
lock.lock();
try {
checkForComodification();
if (index < 0 || index > size)
throw new IndexOutOfBoundsException();
l.add(index+offset, element);
expectedArray = l.getArray();
//元素个数增加
size++;
} finally {
lock.unlock();
}
}
public E remove(int index) {
final ReentrantLock lock = l.lock;
lock.lock();
try {
rangeCheck(index);
checkForComodification();
E result = l.remove(index+offset);
expectedArray = l.getArray();
//元素个数减少
size--;
return result;
} finally {
lock.unlock();
}
}
//检查是否数组越界
private void rangeCheck(int index) {
if (index < 0 || index >= size)
throw new IndexOutOfBoundsException("Index: "+index+
",Size: "+size);
}
//检查数组是否修改
private void checkForComodification() {
if (l.getArray() != expectedArray)
throw new ConcurrentModificationException();
}
public Iterator<E> iterator() {
final ReentrantLock lock = l.lock;
lock.lock();
try {
checkForComodification();
return new COWSubListIterator<E>(l, 0, offset, size);
} finally {
lock.unlock();
}
}
通过代码分析可知,如果不是subList修改导致的array属性变更,再使用subList时就会报错,即使用subList方法返回的List时,不能通过其他方法同时修改父List。COWSubListIterator也是个内部类,基于父List返回的ListIterator实例实现的,其实现如下:
private static class COWSubListIterator<E> implements ListIterator<E> {
private final ListIterator<E> it;
private final int offset;
private final int size;
COWSubListIterator(List<E> l, int index, int offset, int size) {
this.offset = offset;
this.size = size;
//遍历的起始位置从index+offset开始
it = l.listIterator(index+offset);
}
public boolean hasNext() {
return nextIndex() < size;
}
//其实现都是基于父List的listIterator返回的ListIterator实现类
public E next() {
if (hasNext())
return it.next();
else
throw new NoSuchElementException();
}
public boolean hasPrevious() {
return previousIndex() >= 0;
}
public E previous() {
if (hasPrevious())
return it.previous();
else
throw new NoSuchElementException();
}
public int nextIndex() {
//nextIndex方法返回的位置是相对于父List的,对于子List需要减去offset
return it.nextIndex() - offset;
}
public int previousIndex() {
return it.previousIndex() - offset;
}
public void remove() {
throw new UnsupportedOperationException();
}
public void set(E e) {
throw new UnsupportedOperationException();
}
public void add(E e) {
throw new UnsupportedOperationException();
}
@Override
public void forEachRemaining(Consumer<? super E> action) {
Objects.requireNonNull(action);
int s = size;
ListIterator<E> i = it;
while (nextIndex() < s) {
action.accept(i.next());
}
}
}