ArrayList Modification During Iteration
What is the primary issue that arises when the provided code is executed?
import java.util.ArrayList;
import java.util.Iterator;
public class IteratorTest {
static ArrayList<String> list = new ArrayList<>();
public static void main(String[] args) {
list.add("A");
list.add("B");
list.add("C");
Thread t = new Thread(() -> {
synchronized(list) {
Iterator<String> it = list.iterator();
while (it.hasNext()) {
String s = it.next();
if (s.equals("B")) {
list.remove(s);
}
}
}
});
t.start();
}
}
A
The use of the synchronized block results in a deadlock
B
The iterator incorrectly returns null values, leading to a NullPointerException
C
The string comparison in the iterator leads to a race condition
D
Directly modifying the list during iteration causes ConcurrentModificationException even within a synchronized block
Question Leaderboard
Not enough data yet to show leaderboard.
APFIVE