| 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
Thread Safety with Static Arrays
< Prev
Next >

Consider the StaticArray class provided below. What is the primary thread-safety issue demonstrated by this code?

public class StaticArray {
    private static int[] values = new int[3];
    
    public static void increaseAll() {
        for (int i = 0; i < values.length; i++) {
            values[i]++;
        }
    }
    
    public static void main(String[] args) {
        Thread t1 = new Thread(() -> StaticArray.increaseAll());
        Thread t2 = new Thread(() -> StaticArray.increaseAll());
        t1.start();
        t2.start();
    }
}
A

The static method increaseAll does not use any synchronization, leading to race conditions when multiple threads modify the static array concurrently.

B

Static arrays in Java are immutable by default, so no race condition exists.

C

The code is safe because both threads modify the same array in a static context without conflict.

D

The use of static prevents the need for instance-level synchronization, which automatically ensures thread safety.

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