| preferred AP College board partner for AP classes
AP Computer Science A/Unit 6: Array
Start Practice TestPractice Test
About Exam
hard Solved by 2 students
Concurrent Array Modification Race Condition
< Prev
Next >

What is the primary issue when multiple threads execute incrementCounter concurrently?

public class CounterArray {
    private int[] counters = new int[3];
    
    public void incrementCounter(int index) {
        counters[index] = counters[index] + 1;
    }
    
    public static void main(String[] args) {
        CounterArray ca = new CounterArray();
        Runnable task = () -> {
            for (int i = 0; i < 1000; i++) {
                ca.incrementCounter(1);
            }
        };
        Thread t1 = new Thread(task);
        Thread t2 = new Thread(task);
        t1.start();
        t2.start();
    }
}
A

The counter update remains thread-safe because the threads increment the same index in a deterministic manner.

B

The array index is out of bounds because counters has only 3 elements.

C

The read-modify-write sequence in incrementCounter is not atomic, leading to race conditions on counters[1].

D

Since counters is an int array, the individual increments are atomic operations by default.

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

Question Leaderboard

Rank
User
Correct Count
Attempt Count
Time
Score
#1busemagngr01 0m 00s -10
#2raufyildirim9511 2m 30s -50
#3namansoin00001 0m 56s -66
Items per page:
10
1 – 3 of 3
No comments yet. Be the first to comment!

AI Tutor

How can I help?

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