Are you taking CLEP classes? Try clep.ai
| preferred AP College board partner for AP classes
AP Computer Science A/Unit 1: Primitive Types
Start Practice TestPractice Test
About Exam
hard Solved by 2 students

Java Concurrency Race Condition

Mark
< 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 deadlock resulting from improper lock ordering.

B

A livelock caused by threads continually yielding in each iteration.

C

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

D

Performance degradation due to excessive synchronization.

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|