Concurrency With Overloaded Methods
What concurrency issue might occur due to 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
Since the unsynchronized method only updates scores[2], it is isolated and does not affect the overall thread safety.
B
Method overloading is disallowed in multithreaded contexts, which is the root cause of the problem.
C
Overloading methods automatically makes them both synchronized, thereby preventing any concurrency issues.
D
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.
Question Leaderboard
Not enough data yet to show leaderboard.
APFIVE