I'm getting a ConcurrentModificationException
since this code is reached by several threads at the same time:
public void flush(Audit... audits) {
// Copy first them on memory
this.pendingAudits.addAll(Arrays.asList(audits));
for (Iterator<Audit> it = this.pendingAudits.iterator(); it.hasNext();) {
// Do something
it.remove();
}
}
I'm getting an iterator over pendingAudits
and I'm removing each element at the same time other threads can add some other audits.
How to solve it elegantly?