Cha*_*ani 1 java concurrency exception
我知道当我尝试修改或从列表中删除时,我会得到此异常,但只是为了从中读取?这里有什么解决方案?
public boolean recieveHello(Neighbor N, HelloMsg H) {
Iterator<Neighbor> I = NTable.iterator();
Iterator<Neighbor> J = NTable.iterator();
if(!J.hasNext()) {
this.NTable.add(N);
}
while(I.hasNext()) {
if(I.next().nid == N.getnid()) { /*EXCEPTION IS HERE*/
//case where the node is already present in the NTable
}
else {
N.setnhrc(0);
this.NTable.add(N);
//case where the node is to be added to the NTable
}
}
return true;
}
Run Code Online (Sandbox Code Playgroud)
顺便说一句,我必须提到NTable是一个arrayList,并且是类的成员,其方法是这样的
我用::解决了这个问题
public boolean recieveHello(Neighbor N, HelloMsg H) {
Iterator<Neighbor> I = NTable.iterator();
Iterator<Neighbor> J = NTable.iterator();
if(!J.hasNext()) {
this.NTable.add(N);
}
boolean flag = false;
for (int i=0; i<NTable.size(); i++) {
if(NTable.get(i).nid == N.getnid()) {
//case where the node is already present in the NTable
}
else {
flag = true;
N.setnhrc(0);
this.NTable.add(N);
//case where the node is to be added to the NTable
}
}
if(flag == true) {
}
return true;
}
Run Code Online (Sandbox Code Playgroud)
那么当你说时,你正在改变列表的大小,同时迭代它
this.NTable.add(N);
Run Code Online (Sandbox Code Playgroud)
因此,请在单独的列表中跟踪要添加的内容,然后在第一次迭代后附加项目.
| 归档时间: |
|
| 查看次数: |
506 次 |
| 最近记录: |