| 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
< Prev
Next >

Which potential concurrency problem does the above code illustrate?

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

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

B

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

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.

No comments yet. Be the first to comment!

AI Tutor

How can I help?

APFIVE © 2020.
Email: [email protected]|Privacy Policy