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

Lock Ordering and Deadlock

Mark
< Prev
Next >

Which potential concurrency problem is illustrated by the provided code?

public class LockOrdering {
    private final Object lockA = new Object();
    private final Object lockB = new Object();
    
    public void methodA() {
        synchronized(lockA) {
            for (int i = 0; i < 100; i++);
            synchronized(lockB) {
                System.out.println("A");
            }
        }
    }
    
    public void methodB() {
        synchronized(lockB) {
            for (int i = 0; i < 100; i++);
            synchronized(lockA) {
                System.out.println("B");
            }
        }
    }
    
    public static void main(String[] args) {
        LockOrdering lo = new LockOrdering();
        Thread t1 = new Thread(() -> lo.methodA());
        Thread t2 = new Thread(() -> lo.methodB());
        t1.start();
        t2.start();
    }
}
A

A deadlock may occur because methodA and methodB acquire locks in different orders.

B

The use of loops within the synchronized blocks eliminates any concurrency issues.

C

The for-loops cause an infinite loop that halts thread progress.

D

A race condition happens due to improper sharing of loop variables.

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|