Java Concurrency Race Condition
Consider the ArrayRace program provided below. Which of the following best describes the primary concurrency issue that may affect its output?
public class ArrayRace {
private int[] data = {0};
public void increment() {
data[0]++;
}
public static void main(String[] args) throws InterruptedException {
ArrayRace ar = new ArrayRace();
Thread t1 = new Thread(() -> { for(int i = 0; i < 1000; i++) ar.increment(); });
Thread t2 = new Thread(() -> { for(int i = 0; i < 1000; i++) ar.increment(); });
t1.start();
t2.start();
t1.join();
t2.join();
System.out.println("Data: " + ar.data[0]);
}
}
A
A livelock caused by threads continually yielding in each iteration.
B
Performance degradation due to excessive synchronization.
C
A race condition due to unsynchronized increments of the shared array element.
D
A deadlock resulting from improper lock ordering.
Question Leaderboard
Not enough data yet to show leaderboard.
APFIVE