ArrayList Thread Synchronization Issue
In the following code, a writer thread synchronizes its additions to a shared ArrayList. What concurrency issue may still occur due to the reader thread’s unsynchronized iteration?
import java.util.ArrayList;
public class SynchronizedIterationTest {
static ArrayList<Integer> list = new ArrayList<>();
public static void main(String[] args) {
Thread writer = new Thread(() -> {
synchronized(list) {
for (int i = 0; i < 50; i++) {
list.add(i);
try { Thread.sleep(10); } catch (InterruptedException e) {}
}
}
});
Thread reader = new Thread(() -> {
for (Integer num : list) {
System.out.println(num);
try { Thread.sleep(15); } catch (InterruptedException e) {}
}
});
writer.start();
reader.start();
}
}
A
Deadlock from nested synchronization
B
NullPointerException from accessing invalid indices
C
Race condition causing duplicate insertions
D
ConcurrentModificationException due to unsynchronized iteration
Question Leaderboard
| Rank | |||||
|---|---|---|---|---|---|
| #1 | gopangjan893 | 1 | 1 | 0m 06s | 94 |
| #2 | jebaksar000 | 1 | 1 | 0m 48s | 52 |
| #3 | lightingstrikes1342 | 0 | 2 | 0m 00s | -20 |
Items per page:
10
1 – 3 of 3
APFIVE