ArrayList Concurrent Modification
Which potential concurrency issue is demonstrated by the execution of the following code snippet?
import java.util.ArrayList;
public class ExceptionThread {
static ArrayList<Integer> list = new ArrayList<>();
public static void main(String[] args) {
list.add(1);
list.add(2);
Thread t = new Thread(() -> {
try {
list.remove(0);
} catch(Exception e) {
System.out.println("Error");
}
});
t.start();
// Main thread iterates concurrently without synchronization
for (Integer i : list) {
System.out.println(i);
}
}
}
A
Deadlock due to the exception handling block locking the entire list
B
Memory barrier violation because of the generic exception catch block
C
Race condition in the computation of element removal
D
Concurrent modification of the list during iteration that may trigger a ConcurrentModificationException
Question Leaderboard
Not enough data yet to show leaderboard.
APFIVE