Java Nested Lock Deadlock
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.
Question Leaderboard
Not enough data yet to show leaderboard.
