I have 3 servers and a client sending messages. And i implement a BFT algorithm. So i have this part of code
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);
}
}
and during the runtime objects are imported and removed from my Map unordered. also i want to mention that unordered is defined:
Map<String, byte[]> unordered = Collections.synchronizedMap(new HashMap<String, byte[]>());
But suddenly it creates this exception:
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)
Any idea why this happens?
EDIT1: I tried to remove that line: unordered.remove(id);
and i got that error:
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)
EDIT2: Also i want to mention that while i iterate unordered ,some new objects may added, while new messages are coming from the client.