我有 3 个服务器和一个发送消息的客户端。我实现了 BFT 算法。 所以我有这部分代码
int tam = 0;
if (unordered.size() <= maxOrderSize) {
tam = unordered.size();
} else {
tam = maxOrderSize;
}
HashMap<String, byte[]> prop = new HashMap<String, byte[]>(tam);
Iterator<String> it = unordered.keySet().iterator();
for (int i = 0; i < tam; i++) {
if (it.hasNext()) {
String id = it.next();
prop.put(id, unordered.get(id));
it.remove();
unordered.remove(id);
}
}
在运行时,对象会无序从我的 map 中导入和删除。 我还想提一下无序的定义:
Map<String, byte[]> unordered = Collections.synchronizedMap(new HashMap<String, byte[]>());
但是突然它创建了这个异常:
java.util.ConcurrentModificationException
at java.util.HashMap$HashIterator.remove(HashMap.java:1456)
at edu.bft.comm.layer.BatchControl.createOrderMessage(BatchControl.java:123)
at edu.bft.comm.layer.BatchControlTPM.run(BatchControlTPM.java:24)
知道为什么会发生这种情况吗?
编辑1:我尝试删除该行:unordered.remove(id);
我得到了这个错误:
java.util.ConcurrentModificationException
at java.util.HashMap$HashIterator.nextNode(HashMap.java:1442)
at java.util.HashMap$KeyIterator.next(HashMap.java:1466)
at edu.bft.comm.layer.BatchControl.createOrderMessage(BatchControl.java:120)
at edu.bft.comm.layer.BatchControlTPM.run(BatchControlTPM.java:24)
编辑2:另外我想提一下,当我迭代无序时,可能会添加一些新对象,而新消息来自客户端。
最佳答案
您正在自己修改 map 。
删除此行:unordered.remove(id);
根据您的编辑,您在迭代 map 时修改了 map ,因此显然这就是问题所在。
doc说:
The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. If the map is modified while an iteration over the set is in progress (except through the iterator's own remove operation), the results of the iteration are undefined.
并且 HashMap 将抛出 ConcurrentModificationException
如您所见。
关于java - 为什么即使使用迭代器也会出现 ConcurrentModificationException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50283718/