Exception "ConcurrentModificationException" et les ArrayList
Bonjour,
Je débute en Java. Je tente de faire fonctionner un petit programme mais j'ai une exception "ConcurrentModificationException" lors de la modification de mes listes. Je sais que cette exception est générée lorsque l'on parcoure une liste par un thread et on l'a modifie avec un autre mais je n'arrive pas à résoudre le problème :
Code:
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 61 62 63 64
| package test;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
public class MonThread implements Runnable {
public List<InfoToSend> listInfo = Collections
.synchronizedList(new ArrayList<InfoToSend>());
public List<InfoToSend> listInfoTmpAdd = Collections
.synchronizedList(new ArrayList<InfoToSend>());
public List<InfoToSend> listInfoTmpDelete = Collections
.synchronizedList(new ArrayList<InfoToSend>());
private static boolean stop = true;
public MonThread(List<InfoToSend> list) {
this.listInfo = list;
}
@Override
public void run() {
synchronized (listInfo) {
while (stop) {
Iterator<InfoToSend> it = this.listInfo.iterator();
while (it.hasNext()) {
InfoToSend info = it.next();
System.out.println("Index : " + info.getIndex() + " Seq : "
+ info.getSeq() + " Desc : " + info.getDesc());
}
stop = false;
}
}
}
public void delete(int seq) {
listInfoTmpDelete.clear();
listInfoTmpDelete.addAll(listInfo);
Iterator<InfoToSend> it = this.listInfoTmpDelete.iterator();
while (it.hasNext()) {
InfoToSend info = it.next();
if (info.getSeq() == seq) {
listInfoTmpDelete.remove(info);
}
}
listInfo.addAll(listInfoTmpDelete);
}
public void add(InfoToSend alerte) {
listInfoTmpAdd.clear();
listInfoTmpAdd.addAll(listInfo);
listInfoTmpAdd.add(alerte);
listInfo.addAll(listInfoTmpAdd);
}
} |
Code:
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
|
package test;
public class InfoToSend {
public int seq;
public int index;
public String desc;
public int getSeq() {
return seq;
}
public void setSeq(int seq) {
this.seq = seq;
}
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
} |
et ma méthode main :
Code:
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
| package test;
import java.util.ArrayList;
import java.util.List;
public class TestCase {
public TestCase() {
}
public static void main(String[] args) {
List<InfoToSend> listInfos = new ArrayList<InfoToSend>();
InfoToSend info1 = new InfoToSend();
info1.setIndex(1);
info1.setSeq(10);
info1.setDesc("Info1");
InfoToSend info2 = new InfoToSend();
info2.setIndex(2);
info2.setSeq(20);
info2.setDesc("Info2");
listInfos.add(info1);
listInfos.add(info2);
MonThread t1 = new MonThread(listInfos);
t1.run();
InfoToSend info3 = new InfoToSend();
info3.setIndex(2);
info3.setSeq(99);
info3.setDesc("Info3");
t1.add(info3);
t1.delete(99);
}
} |
Merci pour votre aide.