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