http://rongmayisheng.com/post/破除迷信java-util-arraylist在foreach循环遍历时可以删除元素
ArrayList是java开发时非常常用的类,常碰到需要对ArrayList循环删除元素的情况。这时候大家都不会使用foreach循环的方式来遍历List,因为它会抛java.util.ConcurrentModificationException异常。比如下面的代码就会抛这个异常:
那是不是在foreach循环时删除元素一定会抛这个异常呢?答案是否定的。
见这个代码:
这段代码和上面的代码只是把要删除的元素的索引换成了4,这个代码就不会抛异常。为什么呢?
接下来先就这个代码做几个实验,把要删除的元素的索引号依次从1到5都试一遍,发现,除了删除4之外,删除其他元素都会抛异常。接着把list的元素个数增加到7试试,这时候可以发现规律是,只有删除倒数第二个元素的时候不会抛出异常,删除其他元素都会抛出异常。
好吧,规律知道了,可以从代码的角度来揭开谜底了。
首先java的foreach循环其实就是根据list对象创建一个Iterator迭代对象,用这个迭代对象来遍历list,相当于list对象中元素的遍历托管给了Iterator,你如果要对list进行增删操作,都必须经过Iterator,否则Iterator遍历时会乱,所以直接对list进行删除时,Iterator会抛出ConcurrentModificationException异常
其实,每次foreach迭代的时候都有两部操作:
- iterator.hasNext() //判断是否有下个元素
- item = iterator.next() //下个元素是什么,并赋值给上面例子中的item变量
hasNext()方法的代码如下:
这时候你会发现这个异常是在next方法的checkForComodification中抛出的,抛出原因是modCount != expectedModCount
- modCount是指这个list对象从new出来到现在被修改次数,当调用List的add或者remove方法的时候,这个modCount都会自动增减;
- expectedModCount是指Iterator现在期望这个list被修改的次数是多少次。
iterator创建的时候modCount被赋值给了expectedModCount,但是调用list的add和remove方法的时候不会同时自动增减expectedModCount,这样就导致两个count不相等,从而抛出异常。
如果想让其不抛出异常,一个办法是让iterator在调用hasNext()方法的时候返回false,这样就不会进到next()方法里了。这里cursor是指当前遍历时下一个元素的索引号。比如删除倒数第二个元素的时候,cursor指向最后一个元素的,而此时删掉了倒数第二个元素后,cursor和size()正好相等了,所以hasNext()返回false,遍历结束,这样就成功的删除了倒数第二个元素了。
破除迷信,foreach循环遍历的时候不能删除元素不是绝对,倒数第二个元素是可以安全删除的~~(当然以上的思路都是建立在list没有被多线程共享的情况下)
原创文章:[破除迷信]java.util.ArrayList在foreach循环遍历时可以删除元素,转载请注明:转载自戎码一生
---------------------------------------------------------------
转自:
http://www.cnblogs.com/dolphin0520/p/3933551.html
参考资料:
http://blog.csdn.net/izard999/article/details/6708738
http://www.2cto.com/kf/201403/286536.html
在前面一篇文章中提到,对Vector、ArrayList在迭代的时候如果同时对其进行修改就会抛出java.util.ConcurrentModificationException异常。下面我们就来讨论以下这个异常出现的原因以及解决办法。
以下是本文目录大纲:
一.ConcurrentModificationException异常出现的原因
二.在单线程环境下的解决办法
三.在多线程环境下的解决方法
若有不正之处请多多谅解,并欢迎批评指正
请尊重作者劳动成果,转载请标明原文链接:
http://www.cnblogs.com/dolphin0520/p/3933551.html
一.ConcurrentModificationException异常出现的原因
先看下面这段代码:
1
2
3
4
5
6
7
8
9
10
11
12
|
public
class
Test {
public
static
void
main(String[] args) {
ArrayList<Integer> list =
new
ArrayList<Integer>();
list.add(
2
);
Iterator<Integer> iterator = list.iterator();
while
(iterator.hasNext()){
Integer integer = iterator.next();
if
(integer==
2
)
list.remove(integer);
}
}
}
|
运行结果:
从异常信息可以发现,异常出现在checkForComodification()方法中。
我们不忙看checkForComodification()方法的具体实现,我们先根据程序的代码一步一步看ArrayList源码的实现:
首先看ArrayList的iterator()方法的具体实现,查看源码发现在ArrayList的源码中并没有iterator()这个方法,那么很显然这个方法应该是其父类或者实现的接口中的方法,我们在其父类AbstractList中找到了iterator()方法的具体实现,下面是其实现代码:
1
2
3
|
public
Iterator<E> iterator() {
return
new
Itr();
}
|
从这段代码可以看出返回的是一个指向Itr类型对象的引用,我们接着看Itr的具体实现,在AbstractList类中找到了Itr类的具体实现,它是AbstractList的一个成员内部类,下面这段代码是Itr类的所有实现:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
private
class
Itr
implements
Iterator<E> {
int
cursor =
0
;
int
lastRet = -
1
;
int
expectedModCount = modCount;
public
boolean
hasNext() {
return
cursor != size();
}
public
E next() {
checkForComodification();
try
{
E next = get(cursor);
lastRet = cursor++;
return
next;
}
catch
(IndexOutOfBoundsException e) {
checkForComodification();
throw
new
NoSuchElementException();
}
}
public
void
remove() {
if
(lastRet == -
1
)
throw
new
IllegalStateException();
checkForComodification();
try
{
AbstractList.
this
.remove(lastRet);
if
(lastRet < cursor)
cursor--;
lastRet = -
1
;
expectedModCount = modCount;
}
catch
(IndexOutOfBoundsException e) {
throw
new
ConcurrentModificationException();
}
}
final
void
checkForComodification() {
if
(modCount != expectedModCount)
throw
new
ConcurrentModificationException();
}
}
|
首先我们看一下它的几个成员变量:
cursor:表示下一个要访问的元素的索引,从next()方法的具体实现就可看出
lastRet:表示上一个访问的元素的索引
expectedModCount:表示对ArrayList修改次数的期望值,它的初始值为modCount。
modCount是AbstractList类中的一个成员变量
1
|
protected
transient
int
modCount =
0
;
|
该值表示对List的修改次数,查看ArrayList的add()和remove()方法就可以发现,每次调用add()方法或者remove()方法就会对modCount进行加1操作。
好了,到这里我们再看看上面的程序:
当调用list.iterator()返回一个Iterator之后,通过Iterator的hashNext()方法判断是否还有元素未被访问,我们看一下hasNext()方法,hashNext()方法的实现很简单:
1
2
3
|
public
boolean
hasNext() {
return
cursor != size();
}
|
如果下一个访问的元素下标不等于ArrayList的大小,就表示有元素需要访问,这个很容易理解,如果下一个访问元素的下标等于ArrayList的大小,则肯定到达末尾了。
然后通过Iterator的next()方法获取到下标为0的元素,我们看一下next()方法的具体实现:
1
2
3
4
5
6
7
8
9
10
11
|
public
E next() {
checkForComodification();
try
{
E next = get(cursor);
lastRet = cursor++;
return
next;
}
catch
(IndexOutOfBoundsException e) {
checkForComodification();
throw
new
NoSuchElementException();
}
}
|
这里是非常关键的地方:首先在next()方法中会调用checkForComodification()方法,然后根据cursor的值获取到元素,接着将cursor的值赋给lastRet,并对cursor的值进行加1操作。初始时,cursor为0,lastRet为-1,那么调用一次之后,cursor的值为1,lastRet的值为0。注意此时,modCount为0,expectedModCount也为0。
接着往下看,程序中判断当前元素的值是否为2,若为2,则调用list.remove()方法来删除该元素。
我们看一下在ArrayList中的remove()方法做了什么:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
public
boolean
remove(Object o) {
if
(o ==
null
) {
for
(
int
index =
0
; index < size; index++)
if
(elementData[index] ==
null
) {
fastRemove(index);
return
true
;
}
}
else
{
for
(
int
index =
0
; index < size; index++)
if
(o.equals(elementData[index])) {
fastRemove(index);
return
true
;
}
}
return
false
;
}
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
;
// Let gc do its work
}
|
通过remove方法删除元素最终是调用的fastRemove()方法,在fastRemove()方法中,首先对modCount进行加1操作(因为对集合修改了一次),然后接下来就是删除元素的操作,最后将size进行减1操作,并将引用置为null以方便垃圾收集器进行回收工作。
那么注意此时各个变量的值:对于iterator,其expectedModCount为0,cursor的值为1,lastRet的值为0。
对于list,其modCount为1,size为0。
接着看程序代码,执行完删除操作后,继续while循环,调用hasNext方法()判断,由于此时cursor为1,而size为0,那么返回true,所以继续执行while循环,然后继续调用iterator的next()方法:
注意,此时要注意next()方法中的第一句:checkForComodification()。
在checkForComodification方法中进行的操作是:
1
2
3
4
|
final
void
checkForComodification() {
if
(modCount != expectedModCount)
throw
new
ConcurrentModificationException();
}
|
如果modCount不等于expectedModCount,则抛出ConcurrentModificationException异常。
很显然,此时modCount为1,而expectedModCount为0,因此程序就抛出了ConcurrentModificationException异常。
到这里,想必大家应该明白为何上述代码会抛出ConcurrentModificationException异常了。
关键点就在于:调用list.remove()方法导致modCount和expectedModCount的值不一致。
注意,像使用for-each进行迭代实际上也会出现这种问题。
二.在单线程环境下的解决办法
既然知道原因了,那么如何解决呢?
其实很简单,细心的朋友可能发现在Itr类中也给出了一个remove()方法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
public
void
remove() {
if
(lastRet == -
1
)
throw
new
IllegalStateException();
checkForComodification();
try
{
AbstractList.
this
.remove(lastRet);
if
(lastRet < cursor)
cursor--;
lastRet = -
1
;
expectedModCount = modCount;
}
catch
(IndexOutOfBoundsException e) {
throw
new
ConcurrentModificationException();
}
}
|
在这个方法中,删除元素实际上调用的就是list.remove()方法,但是它多了一个操作:
1
|
expectedModCount = modCount;
|
因此,在迭代器中如果要删除元素的话,需要调用Itr类的remove方法。
将上述代码改为下面这样就不会报错了:
1
2
3
4
5
6
7
8
9
10
11
12
|
public
class
Test {
public
static
void
main(String[] args) {
ArrayList<Integer> list =
new
ArrayList<Integer>();
list.add(
2
);
Iterator<Integer> iterator = list.iterator();
while
(iterator.hasNext()){
Integer integer = iterator.next();
if
(integer==
2
)
iterator.remove();
//注意这个地方
}
}
}
|
三.在多线程环境下的解决方法
上面的解决办法在单线程环境下适用,但是在多线程下适用吗?看下面一个例子:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
public
class
Test {
static
ArrayList<Integer> list =
new
ArrayList<Integer>();
public
static
void
main(String[] args) {
list.add(
1
);
list.add(
2
);
list.add(
3
);
list.add(
4
);
list.add(
5
);
Thread thread1 =
new
Thread(){
public
void
run() {
Iterator<Integer> iterator = list.iterator();
while
(iterator.hasNext()){
Integer integer = iterator.next();
System.out.println(integer);
try
{
Thread.sleep(
100
);
}
catch
(InterruptedException e) {
e.printStackTrace();
}
}
};
};
Thread thread2 =
new
Thread(){
public
void
run() {
Iterator<Integer> iterator = list.iterator();
while
(iterator.hasNext()){
Integer integer = iterator.next();
if
(integer==
2
)
iterator.remove();
}
};
};
thread1.start();
thread2.start();
}
}
|
运行结果:
有可能有朋友说ArrayList是非线程安全的容器,换成Vector就没问题了,实际上换成Vector还是会出现这种错误。
原因在于,虽然Vector的方法采用了synchronized进行了同步,但是由于Vector是继承的AbstarctList,因此通过Iterator来访问容器的话,事实上是不需要获取锁就可以访问。那么显然,由于使用iterator对容器进行访问不需要获取锁,在多线程中就会造成当一个线程删除了元素,由于modCount是AbstarctList的成员变量,因此可能会导致在其他线程中modCount和expectedModCount值不等。
就比如上面的代码中,很显然iterator是线程私有的,
初始时,线程1和线程2中的modCount、expectedModCount都为0,
当线程2通过iterator.remove()删除元素时,会修改modCount值为1,并且会修改线程2中的expectedModCount的值为1,
而此时线程1中的expectedModCount值为0,虽然modCount不是volatile变量,不保证线程1一定看得到线程2修改后的modCount的值,但是也有可能看得到线程2对modCount的修改,这样就有可能导致线程1中比较expectedModCount和modCount不等,而抛出异常。
因此一般有2种解决办法:
1)在使用iterator迭代的时候使用synchronized或者Lock进行同步;
2)使用并发容器CopyOnWriteArrayList代替ArrayList和Vector。
关于并发容器的内容将在下一篇文章中讲述。
参考资料:
http://blog.csdn.net/izard999/article/details/6708738
http://www.2cto.com/kf/201403/286536.html
-----------------------------------------------------------------------
一、单线程
1. 异常情况举例
只要抛出出现异常,可以肯定的是代码一定有错误的地方。先来看看都有哪些情况会出现ConcurrentModificationException异常,下面以ArrayList remove 操作进行举例:使用的数据集合:
1
2
3
4
5
6
7
|
List<string> myList =
new
ArrayList<string>();
myList.add(
"1"
);
myList.add(
"2"
);
myList.add(
"3"
);
myList.add(
"4"
);
myList.add(
"5"
);</string></string>
|
以下三种情况都会出现异常:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
Iterator<string> it = myList.iterator();
while
(it.hasNext()) {
String value = it.next();
if
(value.equals(
"3"
)) {
myList.remove(value);
// error
}
}
for
(Iterator<string> it = myList.iterator(); it.hasNext();) {
String value = it.next();
if
(value.equals(
"3"
)) {
myList.remove(value);
// error
}
}
for
(String value : myList) {
System. out.println(
"List Value:"
+ value);
if
(value.equals(
"3"
)) {
myList.remove(value);
// error
}
} </string></string>
|
异常信息如下:
Exception in thread "main" java.util.ConcurrentModificationException
at java.util.AbstractList$Itr.checkForComodification(Unknown Source)
at java.util.AbstractList$Itr.next(Unknown Source)
2. 根本原因
以上都有3种出现异常的情况有一个共同的特点,都是使用Iterator进行遍历,且都是通过ArrayList.remove(Object) 进行删除操作。想要找出根本原因,直接查看ArrayList 源码 看为什么出现异常:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
public
class
ArrayList<e>
extends
AbstractList<e>
implements
Cloneable, Serializable, RandomAccess {
@Override
public
boolean
remove(Object object) {
Object[] a = array;
int
s = size;
if
(object !=
null
) {
for
(
int
i =
0
; i < s; i++) {
if
(object.equals(a[i])) {
System.arraycopy(a, i +
1
, a, i, --s - i);
a[s] =
null
;
// Prevent memory leak
size = s;
modCount++;
// 只要删除成功都是累加
return
true
;
}
}
}
else
{
for
(
int
i =
0
; i < s; i++) {
if
(a[i] ==
null
) {
System.arraycopy(a, i +
1
, a, i, --s - i);
a[s] =
null
;
// Prevent memory leak
size = s;
modCount++;
// 只要删除成功都是累加
return
true
;
}
}
}
return
false
;
}
@Override
public
Iterator<e> iterator() {
return
new
ArrayListIterator();
}
private
class
ArrayListIterator
implements
Iterator<e> {
......
// 全局修改总数保存到当前类中
/** The expected modCount value */
private
int
expectedModCount = modCount;
@SuppressWarnings
(
"unchecked"
)
public
E next() {
ArrayList<e> ourList = ArrayList.
this
;
int
rem = remaining;
// 如果创建时的值不相同,抛出异常
if
(ourList.modCount != expectedModCount) {
throw
new
ConcurrentModificationException();
}
if
(rem ==
0
) {
throw
new
NoSuchElementException();
}
remaining = rem -
1
;
return
(E) ourList.array[removalIndex = ourList.size - rem];
}
......
}
} </e></e></e></e></e>
|
List、Set、Map 都可以通过Iterator进行遍历,这里仅仅是通过List举例,在使用其他集合遍历时进行增删操作都需要留意是否会触发ConcurrentModificationException异常。
3. 解决方案
上面列举了会出现问题的几种情况,也分析了问题出现的根本原因,现在来总结一下怎样才是正确的,如果避免遍历时进行增删操作不会出现ConcurrentModificationException异常。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
// 1 使用Iterator提供的remove方法,用于删除当前元素
for
(Iterator<string> it = myList.iterator(); it.hasNext();) {
String value = it.next();
if
(value.equals(
"3"
)) {
it.remove();
// ok
}
}
System. out.println(
"List Value:"
+ myList.toString());
// 2 建一个集合,记录需要删除的元素,之后统一删除
List<string> templist =
new
ArrayList<string>();
for
(String value : myList) {
if
(value.equals(
"3"
)) {
templist.remove(value);
}
}
// 可以查看removeAll源码,其中使用Iterator进行遍历
myList.removeAll(templist);
System. out.println(
"List Value:"
+ myList.toString());
// 3. 使用线程安全CopyOnWriteArrayList进行删除操作
List<string> myList =
new
CopyOnWriteArrayList<string>();
myList.add(
"1"
);
myList.add(
"2"
);
myList.add(
"3"
);
myList.add(
"4"
);
myList.add(
"5"
);
Iterator<string> it = myList.iterator();
while
(it.hasNext()) {
String value = it.next();
if
(value.equals(
"3"
)) {
myList.remove(
"4"
);
myList.add(
"6"
);
myList.add(
"7"
);
}
}
System. out.println(
"List Value:"
+ myList.toString());
// 4. 不使用Iterator进行遍历,需要注意的是自己保证索引正常
for
(
int
i =
0
; i < myList.size(); i++) {
String value = myList.get(i);
System. out.println(
"List Value:"
+ value);
if
(value.equals(
"3"
)) {
myList.remove(value);
// ok
i--;
// 因为位置发生改变,所以必须修改i的位置
}
}
System. out.println(
"List Value:"
+ myList.toString());</string></string></string></string></string></string>
|
输出结果都是:List Value:[1, 2, 4, 5] , 不会出现异常。
以上4种解决办法在单线程中测试完全没有问题,但是如果在多线程中呢?
二、多线程
1. 同步异常情况举例
上面针对ConcurrentModificationException异常在单线程情况下提出了4种解决方案,本来是可以很哈皮的洗洗睡了,但是如果涉及到多线程环境可能就不那么乐观了。下面的例子中开启两个子线程,一个进行遍历,另外一个有条件删除元素:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
final
List<string> myList = createTestData();
new
Thread(
new
Runnable() {
@Override
public
void
run() {
for
(String string : myList) {
System.out.println(
"遍历集合 value = "
+ string);
try
{
Thread.sleep(
100
);
}
catch
(InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
new
Thread(
new
Runnable() {
@Override
public
void
run() {
for
(Iterator<string> it = myList.iterator(); it.hasNext();) {
String value = it.next();
System.out.println(
"删除元素 value = "
+ value);
if
(value.equals(
"3"
)) {
it.remove();
}
try
{
Thread.sleep(
100
);
}
catch
(InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();</string></string>
|
输出结果:
遍历集合 value = 1
删除元素 value = 1
遍历集合 value = 2
删除元素 value = 2
遍历集合 value = 3
删除元素 value = 3
Exception in thread "Thread-0" 删除元素 value = 4
java.util.ConcurrentModificationException
at java.util.AbstractList$Itr.checkForComodification(Unknown Source)
at java.util.AbstractList$Itr.next(Unknown Source)
at list.ConcurrentModificationExceptionStudy$1.run(ConcurrentModificationExceptionStudy.java:42)
at java.lang.Thread.run(Unknown Source)
删除元素 value = 5
结论:
上面的例子在多线程情况下,仅使用单线程遍历中进行删除的第1种解决方案使用it.remove(),但是测试得知4种的解决办法中的1、2、3依然会出现问题。
接着来再看一下 Java Doc对java.util.ConcurrentModificationException异常的描述:
当方法检测到对象的并发修改,但不允许这种修改时,抛出此异常。
说明以上办法在同一个线程执行的时候是没问题的,但是在异步情况下依然可能出现异常。
2. 尝试方案
(1) 在所有遍历增删地方都加上synchronized或者使用Collections.synchronizedList,虽然能解决问题但是并不推荐,因为增删造成的同步锁可能会阻塞遍历操作。(2) 推荐使用ConcurrentHashMap或者CopyOnWriteArrayList。
3. CopyOnWriteArrayList注意事项
(1) CopyOnWriteArrayList不能使用Iterator.remove()进行删除。(2) CopyOnWriteArrayList使用Iterator且使用List.remove(Object);会出现如下异常:
java.lang.UnsupportedOperationException: Unsupported operation remove
at java.util.concurrent.CopyOnWriteArrayList$ListIteratorImpl.remove(CopyOnWriteArrayList.java:804)
4. 解决方案
单线程情况下列出4种解决方案,但是发现在多线程情况下仅有第4种方案才能在多线程情况下不出现问题。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
List<string> myList =
new
CopyOnWriteArrayList<string>();
myList.add(
"1"
);
myList.add(
"2"
);
myList.add(
"3"
);
myList.add(
"4"
);
myList.add(
"5"
);
new
Thread(
new
Runnable() {
@Override
public
void
run() {
for
(String string : myList) {
System.out.println(
"遍历集合 value = "
+ string);
try
{
Thread.sleep(
100
);
}
catch
(InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
new
Thread(
new
Runnable() {
@Override
public
void
run() {
for
(
int
i =
0
; i < myList.size(); i++) {
String value = myList.get(i);
System.out.println(
"删除元素 value = "
+ value);
if
(value.equals(
"3"
)) {
myList.remove(value);
i--;
// 注意
}
try
{
Thread.sleep(
100
);
}
catch
(InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();</string></string>
|
输出结果:
删除元素 value = 1
遍历集合 value = 1
删除元素 value = 2
遍历集合 value = 2
删除元素 value = 3
遍历集合 value = 3
删除元素 value = 4
遍历集合 value = 4
删除元素 value = 5
遍历集合 value = 5
OK,搞定
三、参考资料
《How to Avoid ConcurrentModificationException when using an Iterator》http://www.2cto.com/kf/201403/286536.html
一、单线程
1. 异常情况举例
只要抛出出现异常,可以肯定的是代码一定有错误的地方。先来看看都有哪些情况会出现ConcurrentModificationException异常,下面以ArrayList remove 操作进行举例:使用的数据集合:
1
2
3
4
5
6
7
|
List<string> myList =
new
ArrayList<string>();
myList.add(
"1"
);
myList.add(
"2"
);
myList.add(
"3"
);
myList.add(
"4"
);
myList.add(
"5"
);</string></string>
|
以下三种情况都会出现异常:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
Iterator<string> it = myList.iterator();
while
(it.hasNext()) {
String value = it.next();
if
(value.equals(
"3"
)) {
myList.remove(value);
// error
}
}
for
(Iterator<string> it = myList.iterator(); it.hasNext();) {
String value = it.next();
if
(value.equals(
"3"
)) {
myList.remove(value);
// error
}
}
for
(String value : myList) {
System. out.println(
"List Value:"
+ value);
if
(value.equals(
"3"
)) {
myList.remove(value);
// error
}
} </string></string>
|
异常信息如下:
Exception in thread "main" java.util.ConcurrentModificationException
at java.util.AbstractList$Itr.checkForComodification(Unknown Source)
at java.util.AbstractList$Itr.next(Unknown Source)
2. 根本原因
以上都有3种出现异常的情况有一个共同的特点,都是使用Iterator进行遍历,且都是通过ArrayList.remove(Object) 进行删除操作。想要找出根本原因,直接查看ArrayList 源码 看为什么出现异常:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
public
class
ArrayList<e>
extends
AbstractList<e>
implements
Cloneable, Serializable, RandomAccess {
@Override
public
boolean
remove(Object object) {
Object[] a = array;
int
s = size;
if
(object !=
null
) {
for
(
int
i =
0
; i < s; i++) {
if
(object.equals(a[i])) {
System.arraycopy(a, i +
1
, a, i, --s - i);
a[s] =
null
;
// Prevent memory leak
size = s;
modCount++;
// 只要删除成功都是累加
return
true
;
}
}
}
else
{
for
(
int
i =
0
; i < s; i++) {
if
(a[i] ==
null
) {
System.arraycopy(a, i +
1
, a, i, --s - i);
a[s] =
null
;
// Prevent memory leak
size = s;
modCount++;
// 只要删除成功都是累加
return
true
;
}
}
}
return
false
;
}
@Override
public
Iterator<e> iterator() {
return
new
ArrayListIterator();
}
private
class
ArrayListIterator
implements
Iterator<e> {
......
// 全局修改总数保存到当前类中
/** The expected modCount value */
private
int
expectedModCount = modCount;
@SuppressWarnings
(
"unchecked"
)
public
E next() {
ArrayList<e> ourList = ArrayList.
this
;
int
rem = remaining;
// 如果创建时的值不相同,抛出异常
if
(ourList.modCount != expectedModCount) {
throw
new
ConcurrentModificationException();
}
if
(rem ==
0
) {
throw
new
NoSuchElementException();
}
remaining = rem -
1
;
return
(E) ourList.array[removalIndex = ourList.size - rem];
}
......
}
} </e></e></e></e></e>
|
List、Set、Map 都可以通过Iterator进行遍历,这里仅仅是通过List举例,在使用其他集合遍历时进行增删操作都需要留意是否会触发ConcurrentModificationException异常。
3. 解决方案
上面列举了会出现问题的几种情况,也分析了问题出现的根本原因,现在来总结一下怎样才是正确的,如果避免遍历时进行增删操作不会出现ConcurrentModificationException异常。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
// 1 使用Iterator提供的remove方法,用于删除当前元素
for
(Iterator<string> it = myList.iterator(); it.hasNext();) {
String value = it.next();
if
(value.equals(
"3"
)) {
it.remove();
// ok
}
}
System. out.println(
"List Value:"
+ myList.toString());
// 2 建一个集合,记录需要删除的元素,之后统一删除
List<string> templist =
new
ArrayList<string>();
for
(String value : myList) {
if
(value.equals(
"3"
)) {
templist.remove(value);
}
}
// 可以查看removeAll源码,其中使用Iterator进行遍历
myList.removeAll(templist);
System. out.println(
"List Value:"
+ myList.toString());
// 3. 使用线程安全CopyOnWriteArrayList进行删除操作
List<string> myList =
new
CopyOnWriteArrayList<string>();
myList.add(
"1"
);
myList.add(
"2"
);
myList.add(
"3"
);
myList.add(
"4"
);
myList.add(
"5"
);
Iterator<string> it = myList.iterator();
while
(it.hasNext()) {
String value = it.next();
if
(value.equals(
"3"
)) {
myList.remove(
"4"
);
myList.add(
"6"
);
myList.add(
"7"
);
}
}
System. out.println(
"List Value:"
+ myList.toString());
// 4. 不使用Iterator进行遍历,需要注意的是自己保证索引正常
for
(
int
i =
0
; i < myList.size(); i++) {
String value = myList.get(i);
System. out.println(
"List Value:"
+ value);
if
(value.equals(
"3"
)) {
myList.remove(value);
// ok
i--;
// 因为位置发生改变,所以必须修改i的位置
}
}
System. out.println(
"List Value:"
+ myList.toString());</string></string></string></string></string></string>
|
输出结果都是:List Value:[1, 2, 4, 5] , 不会出现异常。
以上4种解决办法在单线程中测试完全没有问题,但是如果在多线程中呢?
二、多线程
1. 同步异常情况举例
上面针对ConcurrentModificationException异常在单线程情况下提出了4种解决方案,本来是可以很哈皮的洗洗睡了,但是如果涉及到多线程环境可能就不那么乐观了。下面的例子中开启两个子线程,一个进行遍历,另外一个有条件删除元素:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
final
List<string> myList = createTestData();
new
Thread(
new
Runnable() {
@Override
public
void
run() {
for
(String string : myList) {
System.out.println(
"遍历集合 value = "
+ string);
try
{
Thread.sleep(
100
);
}
catch
(InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
new
Thread(
new
Runnable() {
@Override
public
void
run() {
for
(Iterator<string> it = myList.iterator(); it.hasNext();) {
String value = it.next();
System.out.println(
"删除元素 value = "
+ value);
if
(value.equals(
"3"
)) {
it.remove();
}
try
{
Thread.sleep(
100
);
}
catch
(InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();</string></string>
|
输出结果:
遍历集合 value = 1
删除元素 value = 1
遍历集合 value = 2
删除元素 value = 2
遍历集合 value = 3
删除元素 value = 3
Exception in thread "Thread-0" 删除元素 value = 4
java.util.ConcurrentModificationException
at java.util.AbstractList$Itr.checkForComodification(Unknown Source)
at java.util.AbstractList$Itr.next(Unknown Source)
at list.ConcurrentModificationExceptionStudy$1.run(ConcurrentModificationExceptionStudy.java:42)
at java.lang.Thread.run(Unknown Source)
删除元素 value = 5
结论:
上面的例子在多线程情况下,仅使用单线程遍历中进行删除的第1种解决方案使用it.remove(),但是测试得知4种的解决办法中的1、2、3依然会出现问题。
接着来再看一下 Java Doc对java.util.ConcurrentModificationException异常的描述:
当方法检测到对象的并发修改,但不允许这种修改时,抛出此异常。
说明以上办法在同一个线程执行的时候是没问题的,但是在异步情况下依然可能出现异常。
2. 尝试方案
(1) 在所有遍历增删地方都加上synchronized或者使用Collections.synchronizedList,虽然能解决问题但是并不推荐,因为增删造成的同步锁可能会阻塞遍历操作。(2) 推荐使用ConcurrentHashMap或者CopyOnWriteArrayList。
3. CopyOnWriteArrayList注意事项
(1) CopyOnWriteArrayList不能使用Iterator.remove()进行删除。(2) CopyOnWriteArrayList使用Iterator且使用List.remove(Object);会出现如下异常:
java.lang.UnsupportedOperationException: Unsupported operation remove
at java.util.concurrent.CopyOnWriteArrayList$ListIteratorImpl.remove(CopyOnWriteArrayList.java:804)
4. 解决方案
单线程情况下列出4种解决方案,但是发现在多线程情况下仅有第4种方案才能在多线程情况下不出现问题。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
List<string> myList =
new
CopyOnWriteArrayList<string>();
myList.add(
"1"
);
myList.add(
"2"
);
myList.add(
"3"
);
myList.add(
"4"
);
myList.add(
"5"
);
new
Thread(
new
Runnable() {
@Override
public
void
run() {
for
(String string : myList) {
System.out.println(
"遍历集合 value = "
+ string);
try
{
Thread.sleep(
100
);
}
catch
(InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
new
Thread(
new
Runnable() {
@Override
public
void
run() {
for
(
int
i =
0
; i < myList.size(); i++) {
String value = myList.get(i);
System.out.println(
"删除元素 value = "
+ value);
if
(value.equals(
"3"
)) {
myList.remove(value);
i--;
// 注意
}
try
{
Thread.sleep(
100
);
}
catch
(InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();</string></string>
|
输出结果:
删除元素 value = 1
遍历集合 value = 1
删除元素 value = 2
遍历集合 value = 2
删除元素 value = 3
遍历集合 value = 3
删除元素 value = 4
遍历集合 value = 4
删除元素 value = 5
遍历集合 value = 5
OK,搞定