Java Thread Synchronization Scope
Consider the BatchUpdater class provided below. If multiple threads call the updateData() method concurrently, which statement best describes the most likely outcome for the data array?
public class BatchUpdater {
private int[] data = new int[100];
public void updateData() {
for (int i = 0; i < data.length; i++) {
synchronized(this) {
data[i] += 1;
}
}
}
}
A
The program will throw an exception due to concurrent modifications of an array.
B
A race condition is completely prevented due to the synchronized block in every iteration.
C
Deadlock will occur because each iteration synchronizes on the same object repeatedly.
D
Other threads might observe a partially updated array state since synchronization is applied per iteration rather than for the whole loop.
Question Leaderboard
Not enough data yet to show leaderboard.
