| preferred AP College board partner for AP classes
hard
Concurrency Risk With Compound Conditions
< Prev
Next >

Consider the following ConditionCheck class. What is the primary concurrency risk associated with the compound boolean expression in the useResource method?

public class ConditionCheck {
    private int resource = 0;
    
    public void useResource() {
        if (resource > 0 && resource < 100) {
            resource += 10;
            System.out.println("Resource updated: " + resource);
        }
    }
    
    public void setResource(int value) {
        resource = value;
    }
    
    public static void main(String[] args) {
        ConditionCheck cc = new ConditionCheck();
        Thread t1 = new Thread(() -> {
            for(int i = 0; i < 10; i++){
                cc.useResource();
            }
        });
        Thread t2 = new Thread(() -> cc.setResource(50));
        t1.start(); 
        t2.start();
    }
}
A

The evaluation of the compound condition and the subsequent update are not atomic, leading to potential race conditions when multiple threads access ‘resource’.

B

The use of the && operator provides thread safety, so there is no concurrency issue.

C

A deadlock will occur because checking two conditions simultaneously locks the resource indefinitely.

D

The compound condition always evaluates to true, so it introduces no risk.

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: apfive@apfive.org|Privacy Policy