Are you taking CLEP classes? Try clep.ai
| 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

Mark
< 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 code is safe because both threads modify the same array in a static context without conflict.

B

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

C

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

D

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

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.

AI Tutor

How can I help?

APFIVE © 2020.
Email: apfive@apfive.org|Privacy Policy|