之前在做一些业务逻辑时需要遍历字符串后添加或删除某个符合条件的值,了解到fail-fast的设计思想,下面简单探讨一下。
fail-fast 是一种通用的系统设计思想,一旦检测到可能会发生错误,就立马抛出异常,程序将不再往下执行。
很多时候,我们会把 fail-fast 归类为 Java 集合框架的一种错误检测机制,但其实 fail-fast 并不是 Java 集合框架特有的机制。
@Test
public void failRemove(){
List<String> list = new ArrayList<>();
list.add("白牛");
list.add("沉默");
list.add("潮汐");
for (String str : list) {
if ("白牛".equals(str)) {
list.remove(str);
}
}
System.out.println(list);
}
这段看起来没有问题的代码运行时却报错了
根据错误的堆栈信息,我们可以定位到 ArrayList 的第 901 行代码
final void checkForComodification() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
}
也就是说,remove 的时候触发执行了 checkForComodification
方法,该方法对 modCount 和 expectedModCount 进行了比较,发现两者不等,就抛出了 ConcurrentModificationException
异常。
为什么会执行 checkForComodification
方法呢?
这是因为使用了for-each,for-each 本质上是个语法糖(
就相当于汉语里的成语。即,用更简练的言语表达较复杂的含义。在得到广泛接受的情况之下,可以提升交流的效率
),底层是通过迭代器 Iterator 配合 while 循环实现的
/**
* Returns an iterator over the elements in this list in proper sequence.
*
* <p>The returned iterator is <a href="#fail-fast"><i>fail-fast</i></a>.
*
* @return an iterator over the elements in this list in proper sequence
*/
public Iterator<E> iterator() {
return new Itr();
}
内部类 Itr 实现了 Iterator 接口
private class Itr implements Iterator<E> {
int cursor; // index of next element to return
int lastRet = -1; // index of last element returned; -1 if no such
int expectedModCount = modCount;
Itr() {}
public boolean hasNext() {
return cursor != size;
}
@SuppressWarnings("unchecked")
public E next() {
checkForComodification();
int i = cursor;
if (i >= size)
throw new NoSuchElementException();
Object[] elementData = ArrayList.this.elementData;
if (i >= elementData.length)
throw new ConcurrentModificationException();
cursor = i + 1;
return (E) elementData[lastRet = i];
}
}
也就是说 new Itr()
的时候 expectedModCount 被赋值为 modCount,而 modCount 是 List 的一个成员变量,表示集合被修改的次数。由于 list 此前执行了 3 次 add 方法。
- add 方法调用 ensureCapacityInternal 方法
- ensureCapacityInternal 方法调用 ensureExplicitCapacity 方法
- ensureExplicitCapacity 方法中会执行
modCount++
所以 modCount 的值在经过三次 add 后为 3,于是 new Itr()
后 expectedModCount 的值也为 3。
执行第一次循环时,发现“沉默王二”等于 str,于是执行 list.remove(str)
。
- remove 方法调用 fastRemove 方法
- fastRemove 方法中会执行
modCount++
private void fastRemove(int index) {
modCount++;
int numMoved = size - index - 1;
if (numMoved > 0)
System.arraycopy(elementData, index+1, elementData, index,
numMoved);
elementData[--size] = null; // clear to let GC do its work
}
modCount 的值变成了 4。
执行第二次循环时,会执行 Itr 的 next 方法(String str = (String) var3.next();
),next 方法就会调用 checkForComodification
方法,此时 expectedModCount 为 3,modCount 为 4,就只好抛出 ConcurrentModificationException 异常了。
那其实在阿里巴巴的 Java 开发手册里也提到了,不要在 for-each 循环里进行元素的 remove/add 操作。remove 元素请使用 Iterator 方式。
那该如何正确地删除元素呢?
1)remove 后 break
List<String> list = new ArrayList<>();
list.add("白牛");
list.add("沉默");
list.add("潮汐");
for (String str : list) {
if ("沉默".equals(str)) {
list.remove(str);
break;
}
}
break 后循环就不再遍历了,意味着 Iterator 的 next 方法不再执行了,也就意味着 checkForComodification
方法不再执行了,所以异常也就不会抛出了。
但是呢,当 List 中有重复元素要删除的时候,break 就不合适了。
2)for 循环
List<String> list = new ArrayList<>();
list.add("白牛");
list.add("沉默");
list.add("潮汐");
for (int i = 0, n = list.size(); i < n; i++) {
String str = list.get(i);
if ("沉默".equals(str)) {
list.remove(str);
}
}
for 循环虽然可以避开 fail-fast 保护机制,也就说 remove 元素后不再抛出异常;但是呢,这段程序在原则上是有问题的。为什么呢?
第一次循环的时候,i 为 0,list.size()
为 3,当执行完 remove 方法后,i 为 1,list.size()
却变成了 2,因为 list 的大小在 remove 后发生了变化,也就意味着“沉默”这个元素被跳过了。
remove 之前 list.get(1)
为“白牛”;但 remove 之后 list.get(1)
变成了“潮汐”,而 list.get(0)
变成了“沉默”。
3)使用 Iterator
List<String> list = new ArrayList<>();
list.add("白牛");
list.add("沉默");
list.add("潮汐");
Iterator<String> itr = list.iterator();
while (itr.hasNext()) {
String str = itr.next();
if ("沉默".equals(str)) {
itr.remove();
}
}
为什么使用 Iterator 的 remove 方法就可以避开 fail-fast 保护机制呢?看一下 remove 的源码就明白了。
public void remove() {
if (lastRet < 0)
throw new IllegalStateException();
checkForComodification();
try {
ArrayList.this.remove(lastRet);
cursor = lastRet;
lastRet = -1;
expectedModCount = modCount;
} catch (IndexOutOfBoundsException ex) {
throw new ConcurrentModificationException();
}
}
删除完会执行 expectedModCount = modCount
,保证了 expectedModCount 与 modCount 的同步。
简单地总结一下,fail-fast 是一种保护机制,可以通过 for-each 循环删除集合的元素的方式验证这种保护机制。
那也就是说,for-each 本质上是一种语法糖,遍历集合时很方面,但并不适合拿来操作集合中的元素(增删)。