| preferred AP College board partner for AP classes
AP Computer Science A/Unit 1: Primitive Types
Start Practice TestPractice Test
About Exam
hard
Java Concurrency Race Condition
< Prev
Next >

Consider the ArrayRace program provided below. Which of the following best describes the primary concurrency issue that may affect its output?

public class ArrayRace {
    private int[] data = {0};
    
    public void increment() {
        data[0]++;
    }
    
    public static void main(String[] args) throws InterruptedException {
        ArrayRace ar = new ArrayRace();
        Thread t1 = new Thread(() -> { for(int i = 0; i < 1000; i++) ar.increment(); });
        Thread t2 = new Thread(() -> { for(int i = 0; i < 1000; i++) ar.increment(); });
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println("Data: " + ar.data[0]);
    }
}
A

A livelock caused by threads continually yielding in each iteration.

B

Performance degradation due to excessive synchronization.

C

A race condition due to unsynchronized increments of the shared array element.

D

A deadlock resulting from improper lock ordering.

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