-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTask2Analysis.java
More file actions
34 lines (27 loc) · 1.17 KB
/
Copy pathTask2Analysis.java
File metadata and controls
34 lines (27 loc) · 1.17 KB
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
/*
Task 2 - ConcurrentModificationException Analysis
1. Exact cause:
ConcurrentModificationException occurs when a collection is structurally modified while it is being iterated
by an Iterator/enhanced-for loop, and the modification is not performed through that same Iterator. ArrayList's
iterator is fail-fast and detects that its expected modification count no longer matches the list's actual
modification count.
2. Most likely code pattern at StatementProcessorService.java:142:
for (Transaction transaction : transactions) {
if (shouldRemove(transaction)) {
transactions.remove(transaction);
}
}
The enhanced-for loop internally uses an Iterator, but transactions.remove(transaction) modifies the ArrayList
directly while the Iterator is still active.
3. Minimal safe code change:
Iterator<Transaction> iterator = transactions.iterator();
while (iterator.hasNext()) {
Transaction transaction = iterator.next();
if (shouldRemove(transaction)) {
iterator.remove();
}
}
The key minimal fix is to use iterator.remove() instead of list.remove(...) while iterating.
*/
class Task2Analysis {
}