Concurrency Race Condition With 2D Array
Consider the ArrayUpdater class shown above. If two threads simultaneously call the fill method on the same ArrayUpdater object with different arguments for value, which concurrency issue is most likely to occur?
public class ArrayUpdater {
private int[][] data = new int[4][4];
public void fill(int value) {
for (int i = 0; i < data.length; i++)
for (int j = 0; j < data[i].length; j++)
data[i][j] = value;
}
public int[][] getData() {
return data;
}
public static void main(String[] args) {
ArrayUpdater updater = new ArrayUpdater();
Thread t1 = new Thread(() -> updater.fill(1));
Thread t2 = new Thread(() -> updater.fill(2));
t1.start();
t2.start();
}
}
A
Null pointer exception because the data array is not initialized.
B
Race condition that results in a 2D array with a mix of 1s and 2s due to non-atomic fill operations.
C
Deadlock due to competing locks on the data array.
D
Array index bounds exception due to incorrect loop boundaries.
Question Leaderboard
Not enough data yet to show leaderboard.
APFIVE