| preferred AP College board partner for AP classes
AP Computer Science A/Unit 5: Writing Classes
Start Practice TestPractice Test
About Exam
hard
Java Nested Lock Deadlock
< Prev
Next >

When methodA() and methodB() are executed concurrently by different threads, what potential concurrency issue might arise?

public class DeadlockDemo {
    private final Object lock1 = new Object();
    private final Object lock2 = new Object();
    
    public void methodA() {
        synchronized(lock1) {
            try { Thread.sleep(50); } catch (InterruptedException e) {}
            synchronized(lock2) {
                // Critical section
            }
        }
    }
    
    public void methodB() {
        synchronized(lock2) {
            try { Thread.sleep(50); } catch (InterruptedException e) {}
            synchronized(lock1) {
                // Critical section
            }
        }
    }
}
A

Memory visibility issues because changes aren’t propagated between threads.

B

Deadlock due to nested locks being acquired in opposite orders.

C

Race condition due to unsynchronized access to shared variables.

D

Thread starvation caused by high-priority threads monopolizing resources.

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