Concurrency With Overloaded Methods
Consider the OverloadArray class provided below. Which statement best describes a potential concurrency issue arising from the overloaded updateScores methods?
public class OverloadArray {
private int[] scores = {10, 20, 30};
public synchronized void updateScores() {
scores[0] += 5;
scores[1] += 5;
}
public void updateScores(int index) {
scores[index] += 10;
}
public static void main(String[] args) {
OverloadArray oa = new OverloadArray();
Thread t1 = new Thread(() -> oa.updateScores());
Thread t2 = new Thread(() -> oa.updateScores(2));
t1.start();
t2.start();
}
}
A
Method overloading is disallowed in multithreaded contexts, which is the root cause of the problem.
B
Since the unsynchronized method only updates scores[2], it is isolated and does not affect the overall thread safety.
C
The overloaded updateScores(int) method is not synchronized, so its concurrent call with the synchronized updateScores() may result in unsynchronized access to the scores array.
D
Overloading methods automatically makes them both synchronized, thereby preventing any concurrency issues.
Question Leaderboard
Not enough data yet to show leaderboard.
