Java Thread Synchronization Scope
If multiple threads call updateData() simultaneously, what issue is most likely to occur with respect to the array ‘data’?
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
Other threads might observe a partially updated array state since synchronization is applied per iteration rather than for the whole loop.
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
The program will throw an exception due to concurrent modifications of an array.
Question Leaderboard
Not enough data yet to show leaderboard.
APFIVE