| preferred AP College board partner for AP classes
hard Solved by 2 students
Java Thread Race Condition
< Prev
Next >

What potential concurrency issue exists in the code above?

public class Checker {
    private int data = 0;
    
    public void updateData() {
        if(data == 0) {
            data = 100;
        }
    }
    
    public void printData() {
        System.out.println(data);
    }
    
    public static void main(String[] args) {
        Checker chk = new Checker();
        Thread t1 = new Thread(() -> chk.updateData());
        Thread t2 = new Thread(() -> chk.printData());
        t1.start();
        t2.start();
    }
}
A

The data variable should be declared as final to prevent race conditions.

B

A deadlock might occur because printData() is not synchronized.

C

The if check and subsequent assignment in updateData() are not synchronized, which can lead to a race condition where printData() may read a stale or inconsistent value.

D

There is no issue because the unsynchronized read in printData() doesn’t impact the write in updateData().

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
#1mahmoudjibrin0811 0m 00s 100
#2angelajxi1111 0m 00s 100
#3mali.nehme01 0m 00s -10
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