Are you taking CLEP classes? Try clep.ai
| 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

Mark
< Prev
Next >

Consider the following DeadlockDemo class. If methodA() and methodB() are executed concurrently by separate threads, which potential concurrency issue is demonstrated?

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

Thread starvation caused by high-priority threads monopolizing resources.

B

Race condition due to unsynchronized access to shared variables.

C

Deadlock due to nested locks being acquired in opposite orders.

D

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

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|