| preferred AP College board partner for AP classes
AP Computer Science A/Unit 6: Array
Start Practice TestPractice Test
About Exam
hard Solved by 1 students
Concurrency With Overloaded Methods
< Prev
Next >

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.

Hint
Did You Know?
Explain Why
Explain All Answers
Check Answer
Show Correct Answer
Report Question

Question Leaderboard

Not enough data yet to show leaderboard.

No comments yet. Be the first to comment!

AI Tutor

How can I help?

APFIVE © 2020.
Email: [email protected]|Privacy Policy