Java Concurrency With Static Arrays
Given the code segment below, what is the primary concurrency issue related to the static grid variable, and how can this issue be resolved?
public class StaticMatrix {
private static int[][] grid = new int[3][3];
public static void updateGrid(int value) {
for (int i = 0; i < grid.length; i++)
for (int j = 0; j < grid[i].length; j++)
grid[i][j] = value;
}
public static int readCell(int i, int j) {
return grid[i][j];
}
public static void main(String[] args) {
Thread t1 = new Thread(() -> StaticMatrix.updateGrid(5));
Thread t2 = new Thread(() -> {
System.out.println(StaticMatrix.readCell(1,1));
});
t1.start();
t2.start();
}
}
A
Deadlock caused by static variables; marking grid as volatile would resolve it.
B
Race condition due to unsynchronized static access; adding synchronization around grid accesses would resolve it.
C
Memory leak due to the static grid; removing static would resolve it.
D
Array bounds exception because static grids require explicit locking during initialization.
Question Leaderboard
Not enough data yet to show leaderboard.
